CentOS7.6安装MySQL5.7(建议按照目录结构执行)
1、下载并安装MySQL官方的 Yum Repository
[root@VM-16-6-centos ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
[root@VM-16-6-centos ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm
2、安装MySQL服务器
[root@VM-16-6-centos ~]# yum -y install mysql-community-server
2.1、遇到报错
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql57-community-release-el7-10.noarch (@/mysql57-community-release-el7-10.noarch)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-libs-compat-5.7.37-1.el7.x86_64.rpm is not installed
Failing package is: mysql-community-libs-compat-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
卸载重装 (注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# yum remove mysql mysql-server mysql-libs mysql-server
查看残留文件(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# rpm -qa | grep -i mysql
mysql57-community-release-el7-10.noarch
清除残留文件(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# yum remove mysql57-community-release.noarch
查找残留目录(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# whereis mysql
mysql: /usr/lib64/mysql
逐条删除目录(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# rm -rf /usr/lib64/mysql
开始下载(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@VM-16-6-centos ~]# yum -y install mysql57-community-release-el7-11.noarch.rpm
查看安装效果(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# yum repolist enabled | grep mysql.*
mysql-connectors-community/x86_64 MySQL Connectors Community 230
mysql-tools-community/x86_64 MySQL Tools Community 138
mysql57-community/x86_64 MySQL 5.7 Community Server 564
安装MySQL服务器(注意:这里是过程可不学,建议按照目录结构进行)
[root@VM-16-6-centos ~]# yum install mysql-community-server
又报错(注意:这里是过程可不学,建议按照目录结构进行)
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-server-5.7.37-1.el7.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
Check that the correct key URLs are configured for this repository.
Failing package is: mysql-community-server-5.7.37-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
翻译:
官方 MySQL 存储库的 GPG 密钥已过期,无法安装或更新 MySQL 包。
为 “MySQL 5.7 社区服务器” 存储库列出的 GPG 密钥已经安装,但它们对于这个包不正确
2.2、解决:导入新key
[root@VM-16-6-centos ~]# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
2.3、再次安装MySQL服务器
[root@VM-16-6-centos ~]# yum install mysql-community-server
结果如图:
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : mysql-community-common-5.7.37-1.el7.x86_64 1/4
Installing : mysql-community-libs-5.7.37-1.el7.x86_64 2/4
Installing : mysql-community-client-5.7.37-1.el7.x86_64 3/4
Installing : mysql-community-server-5.7.37-1.el7.x86_64 4/4
Verifying : mysql-community-server-5.7.37-1.el7.x86_64 1/4
Verifying : mysql-community-libs-5.7.37-1.el7.x86_64 2/4
Verifying : mysql-community-common-5.7.37-1.el7.x86_64 3/4
Verifying : mysql-community-client-5.7.37-1.el7.x86_64 4/4
Installed:
mysql-community-server.x86_64 0:5.7.37-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.37-1.el7 mysql-community-common.x86_64 0:5.7.37-1.el7 mysql-community-libs.x86_64 0:5.7.37-1.el7
Complete!
4、启动MySQL
[root@VM-16-6-centos ~]# systemctl start mysqld.service
5、查看运行状态
[root@VM-16-6-centos ~]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2022-02-26 16:25:08 CST; 36s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 29899 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 29844 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 29902 (mysqld)
CGroup: /system.slice/mysqld.service
└─29902 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Feb 26 16:25:02 VM-16-6-centos systemd[1]: Starting MySQL Server...
Feb 26 16:25:08 VM-16-6-centos systemd[1]: Started MySQL Server.
6、更改密码
6.1、找root的密码
[root@VM-16-6-centos ~]# grep "password" /var/log/mysqld.log
2022-02-26T08:25:04.486198Z 1 [Note] A temporary password is generated for root@localhost: sKuj_4*pj1l2
6.2、进入数据库(密码是上面查出来的sKuj_4*pj1l2,每个人不一样)
[root@VM-16-6-centos ~]# mysql -uroot -p
6.3、修改密码(BY后面那个引号里面的是密码)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
上面报错了,因为密码设置必须要大小写字母数字和特殊符号(,/’;:等),不然不能配置成功,重新改即可。
开启mysql的远程访问
用%代表所有,想要限制就用ip替换%即可;
Pass@123是root账号对应的密码
mysql> grant all privileges on *.* to 'root'@'%' identified by 'Pass@123' with grant option;
6.4、刷新退出
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
7、添加mysql端口3306和Tomcat端口8080
7.1、遇到报错
[root@VM-16-6-centos ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
FirewallD is not running
7.2、解决:开启防火墙
对了,要开启防火墙(为了安全)
Active: inactive (dead) 防火墙没开
Active: active (running) 防火墙已开启
[root@VM-16-6-centos ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@VM-16-6-centos ~]# systemctl start firewalld
[root@VM-16-6-centos ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2022-02-26 16:45:42 CST; 3s ago
7.3、添加端口、重启防火墙
[root@VM-16-6-centos ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
[root@VM-16-6-centos ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@VM-16-6-centos ~]# firewall-cmd --reload
success
8、更改mysql的语言
先查看:如图箭头所指,发现并不是UTF-8
[root@VM-16-6-centos ~]# mysql -uroot -p
mysql> status
mysql> exit
8.1、编辑配置文件
[root@VM-16-6-centos ~]# vi /etc/my.cnf
8.2、按 i 切换编辑模式,需新增箭头所指两处
[client]
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci
8.2、按Esc退出编辑,输入:wq
:wq
8.3、重启mysql
[root@VM-16-6-centos ~]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
8.4、进入mysql查看状态
[root@VM-16-6-centos ~]# mysql -uroot -p
mysql> status
9、测试链接
9.1、连接失败
原因:服务器没有开通端口
解决:以腾讯云为例,登录控制台,设置防火墙规则
9.2、重新连接
10、结束
感谢您的观看~