Linux 安装 MySQL 5.7

MySQL 5.7.22 官方下载地址

清理 MySQL、下载解压

清理

find / -name mysql | xargs rm -rf

下载

wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

解压

tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

重命名

mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql


创建 MySQL 组、用户

查看有没有mysql组

cat /etc/group | grep mysql

查看有没有mysql用户

cat /etc/passwd |grep mysql

没有则创建

groupadd mysql
useradd -r -g mysql mysql

更改 mysql 目录下所有的目录及文件夹所属组合用户

chown -R mysql mysql
chgrp -R mysql mysql
cd mysql
ls -l

1
2
3
4
5
6
7
8
9
drwxr-xr-x.  2 mysql mysql  4096 7月  18 15:04 bin
-rw-r--r--. 1 mysql mysql 17987 3月 4 21:40 COPYING
drwxr-xr-x. 2 mysql mysql 52 7月 18 15:04 docs
drwxr-xr-x. 3 mysql mysql 4096 7月 18 15:04 include
drwxr-xr-x. 5 mysql mysql 4096 7月 18 15:04 lib
drwxr-xr-x. 4 mysql mysql 28 7月 18 15:04 man
-rw-r--r--. 1 mysql mysql 2478 3月 4 21:40 README
drwxr-xr-x. 28 mysql mysql 4096 7月 18 15:04 share
drwxr-xr-x. 2 mysql mysql 86 7月 18 15:08 support-files


修改 my.cnf 配置文件

从 5.7.18 开始不在二进制包中提供 my-default.cnf 文件

vi /etc/my.cnf

my-default.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# For advice on how to change settings please see
#
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

my.cnf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# For advice on how to change settings please see
#
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8
socket = /var/lib/mysql/mysql.sock

[mysqld]
character-set-server = utf8

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql/
datadir = /usr/local/mysql/data/
port = 3306
#server_id =
socket = /var/lib/mysql/mysql.sock

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
max_connections = 1000

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES


初始化 MySQL 数据库

初始化

cd /usr/local/mysql
./bin/mysqld –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data –initialize

输出如下:

1
2
3
4
5
6
7
8
2018-07-18T07:06:49.223422Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-18T07:06:49.223579Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2018-07-18T07:06:49.223591Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2018-07-18T07:06:50.630648Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-18T07:06:51.000475Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-18T07:06:51.028751Z 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: 24e198b5-8a59-11e8-9a33-aeceb5cf96b1.
2018-07-18T07:06:51.031122Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-07-18T07:06:51.045661Z 1 [Note] A temporary password is generated for root@localhost: Ra%p1C(>7J(p

添加 RSA Key

cd /usr/local/mysql
bin/mysql_ssl_rsa_setup –datadir=/usr/local/mysql/data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Generating a 2048 bit RSA private key
..+++
.......................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
....................................................................+++
...........................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
..............................................................................................................................................+++
..................................+++
writing new private key to 'client-key.pem'
-----

编辑 /usr/local/mysql/support-files/mysql.server 文件

vi /usr/local/mysql/support-files/mysql.server

1
2
3
4
...
修改 basedir = /usr/local/mysql/
修改 datadir = /usr/local/mysql/data/
...


启动 MySQL 服务

cd /usr/local/mysql
./support-files/mysql.server start

注意:如果遇到以下问题,执行以下操作
Starting MySQL.Logging to ‘/usr/local/mysql/data/localhost.localdomain.err’.
2018-07-18T07:08:26.909710Z mysqld_safe Directory ‘/var/lib/mysql’ for UNIX socket file don’t exists.

mkdir /var/lib/mysql
chown mysql:mysql /var/lib/mysql
cd /usr/local/mysql
./support-files/mysql.server start

Starting MySQL… SUCCESS!

./support-files/mysql.server stop

Shutting down MySQL… SUCCESS!

./support-files/mysql.server restart

Shutting down MySQL… SUCCESS!
Starting MySQL… SUCCESS!


修改 Root 密码,开启远程访问

cd /usr/local/mysql
./bin/mysqld_safe –skip-grant-tables –skip-networking&

登陆

cd /usr/local/mysql/bin
./mysql -uroot -p

修改密码、授权远程登陆

mysql>
set password=password(“123456”);
use mysql;
update user set host=’%’ where user=’root’;
GRANT ALL PRIVILEGES ON . TO ‘myuser‘@’%’ IDENTIFIED BY ‘mypwd’ WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;

重启 MySQL 服务

service mysql restart;

查看编码

show variables like ‘character%’;


配置 MySQL 服务开机自动启动

拷贝启动文件到 /etc/init.d/ 下并重命令为 mysqld

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

增加执行权限

chmod 755 /etc/init.d/mysqld

检查自启动项列表中没有 mysqld 这个,如果没有就添加 mysqld

chkconfig –list mysqld
chkconfig –add mysqld

设置MySQL在345等级自动启动

chkconfig –level 345 mysqld on

或用这个命令设置开机启动:

chkconfig mysqld on


MySQL 服务的启动/重启/停止

启动 MySQL 服务

service mysqld start

停止 MySQL 服务

service mysqld stop

重启 MySQL 服务

service mysqld restart

If these articles are helpful to you, you can donate comment here.