Fork me on GitHub
Fork me on GitHub

CentOS 7安装mysql 5.7.21(二进制版本)

安装依赖

# yum install libaio* -y

下载安装MySQL二进制版本

MySQL社区版下载地址

1.下载解压

# cd /usr/local/
# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
# tar zxf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
# ln -sv mysql-5.7.21-linux-glibc2.12-x86_64 mysql
‘mysql’ -> ‘mysql-5.7.21-linux-glibc2.12-x86_64’

2.新建用户
运行mysql最好不要用root去运行,而以普通用户身份。添加用户mysql。

# groupadd -r -g 300 mysql
# useradd -g mysql -r -s /sbin/nologin -u 300 mysql
# id mysql
uid=300(mysql) gid=300(mysql) groups=300(mysql)

3.创建目录并授权

# cd /usr/local/mysql
# mkdir data
# touch db.err
# chown -R mysql:mysql .

4.初始化MySQL

# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2018-03-20T10:19:23.020203Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-03-20T10:19:24.877676Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-03-20T10:19:25.111211Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-03-20T10:19:25.181304Z 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: 2a1f5e46-2c28-11e8-8a64-00163e13578e.
2018-03-20T10:19:25.189269Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-03-20T10:19:25.189787Z 1 [Note] A temporary password is generated for root@localhost: 77a.Fue3IeIg

注意mysql临时密码在上面日志的最后一行的末尾:77a.Fue3IeIg,请先记下来。
查看初始化后data目录生成的文件:

# ls data/
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  sys

5.生成RSA私钥,可以跳过此步骤
mysql_ssl_rsa_setup需要openssl支持,用于启用数据量ssl连接,需要进一步配置。

# bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data

6.配置my.cnf
将修改好的配置文件my.cnf传到/etc/目录下。my.cnf内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[mysql]
socket = /usr/local/mysql/mysql.sock
# The MySQL server
[mysqld]
port = 3306
socket = /usr/local/mysql/mysql.sock
#skip-grant-tables
datadir=/usr/local/mysql/data
log-error=/usr/local/mysql/db.err
pid-file=/usr/local/mysql/mysqld.pid
character-set-server = utf8

7.拷贝启动脚本文件

# cp support-files/mysql.server /etc/init.d/mysqld 
# chkconfig --add mysqld
# service mysqld start

8.设置开机启动

# chkconfig mysqld on

9.配置环境变量,配置完重新打开一个shell

# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH

9.登录mysql,设置密码

# mysql -uroot -p77a.Fue3IeIg
密码是上面记录下来的临时密码
MySQL [(none)]> ALTER USER 'root'@'localhost' identified by 'test';
MySQL [(none)]> flush privileges;
MySQL [(none)]> quit;

10.在 Linux 下为了安全,默认是不允许 MySQL 服务器本机以外的机器访问 MySQL 数据库服务的,因此需要重新授权 root 账号。方便其他机器远程访问 MySQL 服务器,MySQL 命令如下:

MySQL [(none)]> grant all privileges on *.* to root@'%' identified by 'Wisedu123@2018';
MySQL [(none)]> flush privileges;
MySQL [(none)]> quit;