hunkier

学习笔记,开源代码,技术分享

CentOS 7 安装 MySQL 5.7

CentOS 7 安装MySQL5.7

一、安装包下载

下载地址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads

国内可以使用163镜像加速下载:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/

选择相应的平台和版本下载

二、安装

1.将下载好的安装到解压到/usr/local目录下

1
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

2.进入/usr/local目录

1
cd /usr/local/

3.为mysql安装目录创建软链接

1
ln -s mysql-5.7.26-linux-glibc2.12-x86_64 mysql

4.为centos添加mysql用户组和mysql用户(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)

1
2
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

5.进入安装mysql软件的目录,命令如下

1
cd /usr/local/mysql

6.修改当前目录拥有者为新建的mysql用户,命令如下:

1
chown -R mysql:mysql ./

7.安装mysql,命令如下:

1
2
./bin/mysqld --lower-case-table-names=1 --user=mysql --tmpdir=/tmp --initialize --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 
# 若需要初始化 root 无密码,则需要将 initialize 改为 initialize-insecure

可能会出现如下错误

1
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决方法

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
41
42
43
 yum install -y libaio
Loaded plugins: fastestmirror
Determining fastest mirrors
base | 3.6 kB 00:00:00
epel | 5.3 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): epel/x86_64/updateinfo | 977 kB 00:00:00
(2/4): extras/7/x86_64/primary_db | 200 kB 00:00:00
(3/4): updates/7/x86_64/primary_db | 5.7 MB 00:00:00
(4/4): epel/x86_64/primary_db | 6.7 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================================================================================================================
Package Arch Version Repository Size
=======================================================================================================================================================================================
Installing:
libaio x86_64 0.3.109-13.el7 base 24 k

Transaction Summary
=======================================================================================================================================================================================
Install 1 Package

Total download size: 24 k
Installed size: 38 k
Downloading packages:
libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : libaio-0.3.109-13.el7.x86_64 1/1
Verifying : libaio-0.3.109-13.el7.x86_64 1/1

Installed:
libaio.x86_64 0:0.3.109-13.el7

Complete!

如果出现如下所示则为安装成功

1
2
3
4
5
6
7
8
9
10
11
12
13
2019-05-20T14:14:00.607305Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-05-20T14:14:00.643051Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-05-20T14:14:00.833733Z 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: 83d6fb34-7b09-11e9-96b7-000c2932f18d.
2019-05-20T14:14:00.892413Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-05-20T14:14:00.893234Z 1 [Note] A temporary password is generated for root@localhost: sBN18?Wq&>>I
2019-05-20T14:14:01.309353Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309376Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309382Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309392Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309395Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309400Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309416Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
2019-05-20T14:14:01.309420Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

其实已经生成了root的临时账号:

1
A temporary password is generated for root@localhost: sBN18?Wq&>>I

8.开启mysql服务,命令如下:

1
./support-files/mysql.server start

或者

1
mysqld_safe --defaults-file=/etc/my.cnf

如果出现文件不存在错误,则说明mysql配置文件/etc/my.cnf中的路径不对,修改内容如下,datadir和socket都修改成mysql的安装目录下,增加[client]板块,用于命令行连接mysql数据库。

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
41
42
43
44
45
46
47
48
49
50
#在etc下新建配置文件my.cnf,并在该文件内添加以下配置
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
skip-name-resolve
#skip-grant-tables
#设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 关闭 mysqlx 插件
mysqlx=0
# 服务端使用的字符集默认为8比特编码的latin1字符集
# 指定编码
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

# 开启ip绑定
bind-address = 0.0.0.0

# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 设置忽略大小写
lower_case_table_names=1
max_allowed_packet=16M

# 主从复制配置
gtid_mode = on
enforce_gtid_consistency = 1
binlog_gtid_simple_recovery = 1
relay_log_recovery = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE


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

#指定客户端连接mysql时的编码设置
[client]
default-character-set=utf8mb4

创建相应目录并赋权

1
2
3
4
mkdir -p /var/log/mysql/
chown -R mysql:mysql /var/log/mysql
mkdir -p /var/run/mysql
chown -R mysql:mysql /var/run/mysql

9.重新启开启mysql服务,如下所示则开启成功!

1
2
./support-files/mysql.server start
Starting MySQL.. SUCCESS!

10.将mysql进程放入系统进程中,命令如下:

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

11.重新启动mysql服务,命令如下:

1
2
3
service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!

修改文件权限

1
chmod 755 /etc/rc.d/init.d/mysqld

设置开机启动

1
chkconfig mysqld on

启动、停止命令

1
service mysqld start/stop

12.配置mysql环境变量

1
2
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin

保存退出,再编译下:

1
source /etc/profile

13.使用随机密码登录mysql数据库,命令如下:

1
mysql -u root -p

输入密码回车可能出现如下错误

1
2
3
mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2)

查看是否系统自带的mariadb

1
2
rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64

卸载系统自带的mariadb

1
yum remove mariadb-libs-5.5.60-1.el7_5.x86_64

可能出现如下错误

1
2
mysql -uroot -p
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory

安装相关依赖

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
41
42
43
44
45
46
47
48
49
50
 yum install  libncurses.so.5
Last metadata expiration check: 0:11:30 ago on Tue 07 Jan 2020 07:57:07 AM CST.
Dependencies resolved.
===================================================================================================================================================================================================================
Package Arch Version Repository Size
===================================================================================================================================================================================================================
Installing:
ncurses-compat-libs i686 6.1-7.20180224.el8 BaseOS 350 k
Installing dependencies:
glibc32 x86_64 2.28-42.1.el8 AppStream 1.5 M
libgcc i686 8.2.1-3.5.el8 BaseOS 84 k
libstdc++ i686 8.2.1-3.5.el8 BaseOS 485 k

Transaction Summary
===================================================================================================================================================================================================================
Install 4 Packages

Total download size: 2.4 M
Installed size: 8.3 M
Is this ok [y/N]: y
Downloading Packages:
(1/4): libgcc-8.2.1-3.5.el8.i686.rpm 72 kB/s | 84 kB 00:01
(2/4): glibc32-2.28-42.1.el8.x86_64.rpm 596 kB/s | 1.5 MB 00:02
(3/4): libstdc++-8.2.1-3.5.el8.i686.rpm 180 kB/s | 485 kB 00:02
(4/4): ncurses-compat-libs-6.1-7.20180224.el8.i686.rpm 227 kB/s | 350 kB 00:01
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 510 kB/s | 2.4 MB 00:04
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : libgcc-8.2.1-3.5.el8.i686 1/4
Running scriptlet: libgcc-8.2.1-3.5.el8.i686 1/4
Installing : glibc32-2.28-42.1.el8.x86_64 2/4
Running scriptlet: glibc32-2.28-42.1.el8.x86_64 2/4
Installing : libstdc++-8.2.1-3.5.el8.i686 3/4
Running scriptlet: libstdc++-8.2.1-3.5.el8.i686 3/4
Installing : ncurses-compat-libs-6.1-7.20180224.el8.i686 4/4
Running scriptlet: ncurses-compat-libs-6.1-7.20180224.el8.i686 4/4
Verifying : glibc32-2.28-42.1.el8.x86_64 1/4
Verifying : libgcc-8.2.1-3.5.el8.i686 2/4
Verifying : libstdc++-8.2.1-3.5.el8.i686 3/4
Verifying : ncurses-compat-libs-6.1-7.20180224.el8.i686 4/4

Installed:
ncurses-compat-libs-6.1-7.20180224.el8.i686 glibc32-2.28-42.1.el8.x86_64 libgcc-8.2.1-3.5.el8.i686 libstdc++-8.2.1-3.5.el8.i686

Complete!

如果是 CentOS 8 还需要执行以下脚本

1
2
ln -s /usr/lib64/libncurses.so.6 /usr/lib64/libncurses.so.5
ln -s /usr/lib64/libtinfo.so.6 /usr/lib64/libtinfo.so.5

输入随机密码登录成功:

1
2
3
4
5
6
7
8
9
10
11
12
13
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2010
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

14.进入mysql操作行,为root用户设置新密码(比如设为123456):

在13条已经登录的终端中输入如下命令:

1
2
3
4
alter user 'root'@'localhost' identified by '123456';
# 或者
use mysql
update user set authentication_string=password('新密码') where user='root';

15.设置允许远程连接数据库,命令如下:

先选择数据库:

1
2
use mysql
update user set user.Host='%' where user.User='root';

查看修改后的值:

1
select user,host from user;

16.刷新权限,命令如下:

1
flush privileges;

17、开启3306防火墙端口,然后即可远程连接mysql(因为我的防火墙是全部关闭,所以省略了这步)

18、如果还是无法远程连接,查看/etc/my.cnf

找到bind-address = 127.0.0.1这一行

1
改为bind-address = 0.0.0.0即可

19、添加新的用户并授权

1
2
3
4
CREATE USER 'developer'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'developer'@'%' WITH GRANT OPTION ;
# REVOKE ALL PRIVILEGES ON db1.* FROM 'developer'@'%' ;
flush privileges;
谢谢你请我喝牛奶

欢迎关注我的其它发布渠道