直接从网易开源镜像站下载镜像(可以用阿里,华为,清华大学等等许多)我随便选了一个

1
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.32-1.el7.x86_64.rpm-bundle.tar

1 安装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
tar -xvf mysql-5.7.33-1.el7.x86_64.rpm-bundle.tar  -C ./mysql-5.7.33


mysql-community-embedded-devel-5.7.21-1.el7.x86_64.rpm

mysql-community-minimal-debuginfo-5.7.21-1.el7.x86_64.rpm

mysql-community-common-5.7.21-1.el7.x86_64.rpm

mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm

mysql-community-embedded-compat-5.7.21-1.el7.x86_64.rpm

mysql-community-server-minimal-5.7.21-1.el7.x86_64.rpm

mysql-community-client-5.7.21-1.el7.x86_64.rpm

mysql-community-server-5.7.21-1.el7.x86_64.rpm

mysql-community-embedded-5.7.21-1.el7.x86_64.rpm

mysql-community-test-5.7.21-1.el7.x86_64.rpm

mysql-community-devel-5.7.21-1.el7.x86_64.rpm

mysql-community-libs-5.7.21-1.el7.x86_64.rpm

解压出来,发现有很多rpm包,我们需要用到的包是:

1
2
3
4
5
6
mysql-community-common-5.7.20-1.el7.x86_64.rpm
mysql-community-libs-5.7.20-1.el7.x86_64.rpm
mysql-community-client-5.7.20-1.el7.x86_64.rpm
mysql-community-server-5.7.20-1.el7.x86_64.rpm

(注意:安装顺序不能错,否则会报错依赖包没有安装!!!! )
1
2
3
# 卸载自带的Mysql-libs mariadb(如果之前安装过mysql,要全都卸载掉)

rpm -qa | grep -i -E mysql\|mariadb | xargs -n1 sudo rpm -e --nodeps

安装mysql依赖

1
2
3
sudo rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm

2)安装mysql-client

1
sudo rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm

3)安装mysql-server

1
sudo rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm

4)启动mysql

1
sudo systemctl start mysqld

5)查看mysql密码

1
sudo cat /var/log/mysqld.log | grep password

2. 配置MySQL

配置只要是root用户+密码,在任何主机上都能登录MySQL数据库。

1)用刚刚查到的密码进入mysql(如果报错,给密码加单引号)

1
mysql -uroot -p 'password'

2)设置复杂密码(由于mysql密码策略,此密码必须足够复杂)

1
2
# 我直接跳过这个了。
set password=password("Qs23=zs32");

3)更改mysql密码策略

1
2
3
set global validate_password_length=4; #定义长度 默认是8

set global validate_password_policy=0; #定义复杂度
  • MYSQL8
1
2
3
4
5
mysql> SET GLOBAL validate_password.policy=LOW;
mysql> SET GLOBAL validate_password.length=8;
mysql> SET GLOBAL validate_password.letter_count=1;
mysql> SET GLOBAL validate_password.digit_count=1;
mysql> SET GLOBAL validate_password.special_char_count=1;

4)设置简单好记的密码

1
2
3
4
set password=password("123456");

set password for 'root'@'localhost'=password('123456'); # 修改root账号登陆密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456'; # 修改root账号登陆密码

5)授权

1
2
3
4
5
6
7
8
9
10
11
12
13
14
###### 方法一 ##########
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '123456' WITH GRANT OPTION;



###### 方法二 ##########
# 进入msyql库
use mysql

# 查询user表
select user, host from user;

# 修改user表,把Host表内容修改为%
update user set host="%" where user="root";

8)刷新

1
flush privileges;

9)退出

1
quit;

3. 一些小坑

  • Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEYerror: Failed dependencies:
1
2
3
4
5
6
7
8
rpm -ivh mysql-community-server-5.7.26-1.el6.x86_64.rpm
warning: mysql-community-server-5.7.26-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
libsasl2.so.2()(64bit) is needed by mysql-community-server-5.7.26-1.el6.x86_64

原因:这是由于yum安装了旧版本的GPG keys造成的 解决办法: --force --nodeps

rpm -ivh mysql-community-server-5.7.26-1.el6.x86_64.rpm --force --nodeps
  • ./mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

类库有问题,然后重新安装了,也是报错,后面重新把包整理了一遍,重新安装,问题解决。

1
2
3
4
5
6
7
8
9
10
11
yum search libaio
yum install libaio
yum -y install numactl
yum install libnuma
yum install ld-linux.so.2
yum install libaio.so.1
yum install libnuma.so.1
yum install libstdc++.so.6
yum install libtinfo.so.5
yum clean all
yum makecache
  • Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKE error: Failed dependencies:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
warning: mysql-community-client-5.7.21-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

error: Failed dependencies:

mariadb-libs is obsoleted by mysql-community-libs-5.7.21-1.el7.x86_64

mariadb-libs is obsoleted by mysql-community-libs-compat-5.7.21-1.el7.x86_64

perl(Data::Dumper) is needed by mysql-community-test-5.7.21-1.el7.x86_64

perl(JSON) is needed by mysql-community-test-5.7.21-1.el7.x86_64



如上的报错,由于centos 7默认是mariadb数据库,再去安装mysql之前要先卸载mariadb

rpm -qa | grep mariadb

mariadb-libs-5.5.41-2.el7_0.x86_64

rpm -e mariadb-libs-5.5.41-2.el7_0.x86_64 --nodeps
  • 使用临时密码登录不进去mysql: [Warning] Using a password on the command line interface can be insec
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
# 1.在root用户下,停止mysql
service mysqld stop

# 2.vi /etc/my.cnf
在[mysqld]的段中加上一句skip-grant-tables

[mysqld]
skip-grant-tables

shift+z+z;保存退出

# 3.重新启动mysql
systemctl start mysqld

# 4.登录修改mysql 的密码

[root@python2 hadoop]# mysql -uroot -p

Enter password:

一直敲回车

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2

Server version: 5.7.21 MySQL Community Server (GPL)

看到welcome登录成功

# 5.FLUSH PRIVILEGES; 执行这条命令刷新

# 6.授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%'IDENTIFIED BY 'mypassword' WITH GRANT OPTION;

Query OK, 0 rows affected, 1 warning (0.01 sec)

# 6:修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00 sec)

# 7.退出 quit;

8. 到vi /etc/my.cnf 删除skip-grant-tables

# 9.登录mysql 成功修改密码并登录
  • 启动mysql服务报错
1
2
3
4
5
6
[root@zhangxxx init.d]# systemctl start mysqld
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

[root@zhangcool-cooler01 init.d]# service mysqld start
Starting mysqld (via systemctl): Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[FAILED]

根据提示检查服务状态

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
[root@zhangxxx init.d]# systemctl status mysqld.service
● mysqld.service - SYSV: MySQL database server.
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2018-07-06 13:43:52 CST; 1min 2s ago
Docs: man:systemd-sysv-generator(8)
Process: 3886 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=1/FAILURE)

Jul 06 13:43:51 zhangcool-cooler01 systemd[1]: Starting SYSV: MySQL database server....
Jul 06 13:43:52 zhangcool-cooler01 mysqld[3886]: MySQL Daemon failed to start.
Jul 06 13:43:52 zhangcool-cooler01 mysqld[3886]: Starting mysqld: [FAILED]
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: mysqld.service: control process exited, code=exited status=1
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: Failed to start SYSV: MySQL database server..
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: Unit mysqld.service entered failed state.
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: mysqld.service failed.

[root@zhangcool-cooler01 init.d]# journalctl -xe
Jul 06 13:42:58 zhangcool-cooler01 polkitd[477]: Unregistered Authentication Agent for unix-process:3597:8111728 (system bus name :1.355, object path
Jul 06 13:43:51 zhangcool-cooler01 polkitd[477]: Registered Authentication Agent for unix-process:3880:8117203 (system bus name :1.356 [/usr/bin/pktt
Jul 06 13:43:51 zhangcool-cooler01 systemd[1]: Starting SYSV: MySQL database server....
-- Subject: Unit mysqld.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has begun starting up.
Jul 06 13:43:52 zhangcool-cooler01 mysqld[3886]: MySQL Daemon failed to start.
Jul 06 13:43:52 zhangcool-cooler01 mysqld[3886]: Starting mysqld: [FAILED]
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: mysqld.service: control process exited, code=exited status=1
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: Failed to start SYSV: MySQL database server..
-- Subject: Unit mysqld.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has failed.
--
-- The result is failed.
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: Unit mysqld.service entered failed state.
Jul 06 13:43:52 zhangcool-cooler01 systemd[1]: mysqld.service failed.
Jul 06 13:43:52 zhangcool-cooler01 polkitd[477]: Unregistered Authentication Agent for unix-process:3880:8117203 (system bus name :1.356, object path

查看mysql日志,分析mysql启动过程有何异常

1
2
3
4
[root@zhangcool-cooler01 init.d]# vi /var/log/mysqld.log
2021-07-06T05:43:52.466691Z 0 [Note] Server socket created on IP: '::'.
2021-07-06T05:43:52.469258Z 0 [ERROR] /usr/sbin/mysqld: Can't create/write to file '/var/run/mysqld/mysqld.pid' (Errcode: 2 - No such file or directory)
2021-07-06T05:43:52.469270Z 0 [ERROR] Can't start server: can't create PID file: No such file or directory

原来/var/run/mysqld目录不存在,所在mysqld.pid无法创建

创建目录/var/run/mysqld,并授权,再次启动mysql成功

1
2
3
4
mkdir -p /var/run/mysqld
ll /var/run/
chown -R mysql.mysql /var/run/mysqld
ll /var/run/
  • warning: mysql-community-libs-5.7.33-1.el7.x86_64.rpm: Header V3SA/SHA1 Signature,key ID 5072e1f5: NOKEY

签名校验失败

直接强制安装

1
rpm -ivh --force --nodeps mysql-community-libs-5.7.33-1.el7.x86_64.rpm