linux下安装mysql

1、查看系统版本,显示我的系统是64位

[root@iz2zedr8770rctry7tq8t7z ~]# uname -a
Linux iz2zedr8770rctry7tq8t7z 3.10.0-514.26.2.el7.x86_64 #1 SMP Tue Jul 4 15:04:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@iz2zedr8770rctry7tq8t7z ~]# 

2、检查是否安装过mysql,我这里显示没有安装过

[root@iz2zedr8770rctry7tq8t7z ~]#  rpm -qa | grep mysql
[root@iz2zedr8770rctry7tq8t7z ~]#

  如果安装过,需要卸载,参考博客:https://www.jianshu.com/p/276d59cbc529

3、下载mysql

在官网:http://dev.mysql.com/downloads/mysql/ 中,选择合适版本的mysql下载:  可以命令行下载,也可以下载完安装包以后,传到linux下

[root@iz2zedr8770rctry7tq8t7z ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz

 4、安装

a:解压安装包

[root@iz2zedr8770rctry7tq8t7z ~]# ls   #解压后目录如下
mysql-5.6.46-linux-glibc2.12-x86_64  mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
[root@iz2zedr8770rctry7tq8t7z ~]# mv mysql-5.6.46-linux-glibc2.12-x86_64 mysql #目录名太长,重命名
[root@iz2zedr8770rctry7tq8t7z ~]# ls
mysql  mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz

b:添加mysql用户组和mysql用户

  先检查是否有mysql用户组和mysql用户,我这里是没有的

[root@iz2zedr8770rctry7tq8t7z mysql]# groups mysql
groups: mysql: no such user

  创建用户和用户组

[root@iz2zedr8770rctry7tq8t7z mysql]# groupadd mysql
[root@iz2zedr8770rctry7tq8t7z mysql]# useradd -r -g mysql mysql
[root@iz2zedr8770rctry7tq8t7z mysql]# groups mysql
mysql : mysql

  将安装目录所有者及所属组改为mysql ,这个根据自己的目录来,我安装到了root目录下,是自己用来练习的

[root@iz2zedr8770rctry7tq8t7z mysql]# chown -R mysql.mysql /root/mysql

  编译安装并初始化mysql,务必记住初始化输出日志末尾的密码(数据库管理员临时密码)

[root@iz2zedr8770rctry7tq8t7z bin]# /root/mysql/bin/mysqld --user=mysql --basedir=/root/mysqll --datadir=/root/mysql/data --initialize
/root/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

  安装过程中,遇到了如上错误,安装如下模块

[root@iz2zedr8770rctry7tq8t7z bin]# yum install libaio

  上面问题解决了,又出现了如下问题,查找网上的解决办法,指定--datadir=/root/mysql/data,我本身初始化就指定了。还有说将my.cnf中read only注释掉,我的cnf文件本身就没有

[ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it.

5、由于报错没解决,自己也不会解决,于是简单粗暴的删除,重新安装

  完全按照博客:https://www.jianshu.com/p/276d59cbc529 安装

  编译安装并初始化mysql,务必记住初始化输出日志末尾的密码(数据库管理员临时密码),是成功的(_oz0u4sfKJ(d)

[root@iz2zedr8770rctry7tq8t7z bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
2019-12-17T02:55:45.321270Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-17T02:55:56.116276Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-12-17T02:55:57.622633Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-17T02:55:57.767239Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c000ee56-2078-11ea-8e2b-00163e14ed06.
2019-12-17T02:55:57.770174Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-12-17T02:55:57.770767Z 1 [Note] A temporary password is generated for root@localhost: _oz0u4sfKJ(d

6、编辑配置文件,编辑配置文件my.cnf,添加配置如下

[root@localhost bin]#  vi /etc/my.cnf

[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_connections=400
innodb_file_per_table=1
#表名大小写不明感,敏感为
lower_case_table_names=1


[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

7、启动服务,报错

[root@iz2zedr8770rctry7tq8t7z mysql]# cd support-files/
[root@iz2zedr8770rctry7tq8t7z support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@iz2zedr8770rctry7tq8t7z support-files]# ./mysql.server start
Starting MySQL.2019-12-17T03:03:23.540783Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
The server quit without updating PID file (/usr/local/mysql[FAILED]2zedr8770rctry7tq8t7z.pid).

  解决办法:查找发现确实没有/var/log/mariadb/mariadb.log这个文件,新建并更改其用户组即可

[root@iz2zedr8770rctry7tq8t7z var]# cd log
[root@iz2zedr8770rctry7tq8t7z log]# ls
anaconda        cron-20191124                 grubby              maillog-20191208   multi-nic-util   secure-20191202   spooler-20191215
audit           cron-20191202                 grubby_prune_debug  maillog-20191215   ntp.log          secure-20191208   tallylog
boot.log        cron-20191208                 journal             messages           ntpstats         secure-20191215   tuned
btmp            cron-20191215                 lastlog             messages-20191124  ppp              spooler           wpa_supplicant.log
btmp-20191201   dmesg                         maillog             messages-20191202  sa               spooler-20191124  wtmp
cloud-init.log  dmesg.old                     maillog-20191124    messages-20191208  secure           spooler-20191202  yum.log
cron            ecs_network_optimization.log  maillog-20191202    messages-20191215  secure-20191124  spooler-20191208
[root@iz2zedr8770rctry7tq8t7z log]# mkdir mariadb
[root@iz2zedr8770rctry7tq8t7z log]# cd mariadb/
[root@iz2zedr8770rctry7tq8t7z mariadb]# touch mariadb.log
[root@iz2zedr8770rctry7tq8t7z mariadb]# cd ..
[root@iz2zedr8770rctry7tq8t7z log]# chown -R mysql:mysql /var/log/mariadb/

  再次启动服务,继续报错,伤心。var/lib/mysql”目录不存在

[root@iz2zedr8770rctry7tq8t7z support-files]# ./mysql.server start
Starting MySQL.2019-12-17T03:13:27.055391Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
The server quit without updating PID file (/usr/local/mysql[FAILED]2zedr8770rctry7tq8t7z.pid).

  解决

[root@iz2zedr8770rctry7tq8t7z lib]# mkdir mysql
[root@iz2zedr8770rctry7tq8t7z lib]# ls
alternatives  cloud  dhclient  games      logrotate  misc   NetworkManager  os-prober  polkit-1  rpm        rsyslog    systemd  yum
authconfig    dbus   dnsmasq   initramfs  machines   mysql  ntp             plymouth   postfix   rpm-state  stateless  tpm
[root@iz2zedr8770rctry7tq8t7z lib]# chmod 777  /var/lib/mysql

  再次启动服务,终于起来了,安装成功

[root@iz2zedr8770rctry7tq8t7z support-files]# ./mysql.server start
Starting MySQL....                                         [  OK  ]

8、添加软链接,并重启mysql服务

[root@iz2zedr8770rctry7tq8t7z support-files]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@iz2zedr8770rctry7tq8t7z support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@iz2zedr8770rctry7tq8t7z support-files]# service mysql restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL..                                           [  OK  ]

9、登录mysql,修改密码(使用临时密码)。没错,又报错了

[root@iz2zedr8770rctry7tq8t7z support-files]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

  解决办法:修改my.cnf配置文件,添加client 。参考:https://blog.csdn.net/zhizhengguan/article/details/86686930

[mysqld]
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
port = 3306
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
max_connections=400
innodb_file_per_table=1
#表名大小写不明感,敏感为
lower_case_table_names=1


[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

10、重新启动并登陆,登陆成功了

[root@iz2zedr8770rctry7tq8t7z support-files]# service mysql restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL..                                           [  OK  ]
[root@iz2zedr8770rctry7tq8t7z support-files]#  mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.24

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

mysql>

11、修改密码

mysql> set password=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

12、开放远程链接

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set user.Host='%' where user.User='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

13、设置开机自动启动

1、将服务文件拷贝到init.d下,并重命名为mysql
[root@localhost /]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
2、赋予可执行权限
[root@localhost /]# chmod +x /etc/init.d/mysqld
3、添加服务
[root@localhost /]# chkconfig --add mysqld
4、显示服务列表
[root@localhost /]# chkconfig --list

注:看到有的博客在步骤7前,修改系统配置文件,可能这就是我各种报错的原因,参考博客:https://www.cnblogs.com/fanshudada/p/9781794.html

#cd /usr/local/mysql/support-files

# cp my-default.cnf /etc/my.cnf
# cp mysql.server /etc/init.d/mysql

# vim /etc/init.d/mysql

原文地址:https://www.cnblogs.com/hzgq/p/12050517.html