1. 通过yum下载mysql包
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
2.安装yum源包
[root@iZ2ze2he0vxbigahympj6zZ]~# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
3.配置yum对应mysql的版本
使用yum repolist all | grep mysql
查看对应的包(community 代表社区版),如下图只有8.0版本的mysql是启动的。如果不配置默认安装的就是mysql8.0版本。
[root@iZ2ze2he0vxbigahympj6zZ]~# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 185
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 123
mysql-tools-community-source MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 229
mysql80-community-source MySQL 8.0 Community Server - disabled
这边使用yum命令来禁用8.0版本的mysql,启用5.7版本的mysql。
yum-config-manager --disable mysql80-community
如果提示:
command not found: yum-config-manager
先安装yum-utils :yum -y install yum-utils
执行会发现enable=0
[root@iZ2ze2he0vxbigahympj6zZ]~# yum-config-manager --disable mysql80-community
Loaded plugins: fastestmirror
============================================================= repo: mysql80-community =============================================================
[mysql80-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
baseurl = http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/
cache = 0
cachedir = /var/cache/yum/x86_64/7/mysql80-community
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage =
enabled = 0
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7/mysql80-community/gpgcadir
gpgcakey =
gpgcheck = True
执行yum-config-manager --enable mysql57-community
root@iZ2ze2he0vxbigahympj6zZ]~# yum-config-manager --enable mysql57-community
Loaded plugins: fastestmirror
============================================================= repo: mysql57-community =============================================================
[mysql57-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
baseurl = http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
cache = 0
cachedir = /var/cache/yum/x86_64/7/mysql57-community
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage =
enabled = 1
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7/mysql57-community/gpgcadir
gpgcakey =
gpgcheck = True
再次执行命令yum repolist all | grep mysql
确定命令是否执行成功。
[root@iZ2ze2he0vxbigahympj6zZ]~# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 185
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 123
mysql-tools-community-source MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 484
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
4.安装Mysql并启动服务
执行安装命令 yum -y install mysql mysql-server
进行安装,后面等待安装完成即可。
(如果报异常:mysql-community-devel-5.7.42-1.el7.x86_64.rpm 的公钥尚未安装
可通过rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
解决)
使用如下命令,启动并查看mysql服务状态。
systemctl start mysqld.service
systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2021-03-19 16:41:49 CST; 8s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 11615 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 11566 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 11620 (mysqld)
CGroup: /system.slice/mysqld.service
└─11620 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Mar 19 16:41:45 iz2ze2he0vxbigahympj6zz systemd[1]: Starting MySQL Server...
Mar 19 16:41:49 iz2ze2he0vxbigahympj6zz systemd[1]: Started MySQL Server.
mysql服务设置开机自启动
systemctl enable mysqld
5.修改mysql默认密码
使用grep 'temporary password' /var/log/mysqld.log
查看系统自动生成的初始密码
2021-03-19T08:41:46.889283Z 1 [Note] A temporary password is generated for root@localhost: W8zug%CAp5cT
使用mysql -uroot -p
进行登录,登录后用ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!'
; 进行修改密码。
修改后成功登陆即可正常使用。
6. 设置可远程访问
上面只是可以在服务器上访问,Mysql默认不允许远程登录,所以必须设置下,并且防火墙开放3306端口(我的是应用服务器,可直接在外部开启);
在mysql中运行:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Root123@' WITH GRANT OPTION;