查看是否已经安装了mysql
rpm -qa|grep mysql #无输出说明没有安装
打开网址:MySQL Yum存储库
复制下载文件的全名:
mysql80-community-release-el7-3.noarch.rpm
下载和安装mysql源
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@iZbp117qjre4vusjrgw7ftZ /]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
--2021-01-05 08:56:39-- https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm [following]
--2021-01-05 08:56:41-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 104.91.72.230
Connecting to repo.mysql.com (repo.mysql.com)|104.91.72.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26024 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-3.noarch.rpm.1’
100%[====================================================================================================================================================================================================================================>] 26,024 62.3KB/s in 0.4s
2021-01-05 08:56:43 (62.3 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm.1’ saved [26024/26024]
安装mysql源
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
[root@iZbp117qjre4vusjrgw7ftZ /]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
查看mysql相关资源
sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
[root@iZbp117qjre4vusjrgw7ftZ /]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
[root@iZbp117qjre4vusjrgw7ftZ /]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 175
mysql-tools-community/x86_64 MySQL Tools Community 120
mysql80-community/x86_64 MySQL 8.0 Community Server 211
选择masql版本
yum repolist all | grep mysql
使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本
使用命令:yum repolist all | grep mysql
,查看当前yum repolist的所有版本
[root@iZbp117qjre4vusjrgw7ftZ /]# 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: 175
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 120
mysql-tools-community-source MySQL Tools Community - Sourc 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: 211
mysql80-community-source MySQL 8.0 Community Server - disabled
切换版本
sudo yum-config-manager --disable mysql80-community
sudo yum-config-manager --enable mysql57-community
[root@iZbp117qjre4vusjrgw7ftZ /]# sudo yum-config-manager --disable mysql80-community
Loaded plugins: fastestmirror, langpacks
========================================================================================================================== repo: mysql80-community ===========================================================================================================================
[mysql80-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/2.1903
baseurl = http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/
cache = 0
cachedir = /var/cache/yum/x86_64/2.1903/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/2.1903/mysql80-community/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/2.1903/mysql80-community/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
hdrdir = /var/cache/yum/x86_64/2.1903/mysql80-community/headers
http_caching = all
includepkgs =
ip_resolve =
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid =
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink =
minrate = 0
mirrorlist =
mirrorlist_expire = 86400
name = MySQL 8.0 Community Server
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/2.1903/mysql80-community
pkgdir = /var/cache/yum/x86_64/2.1903/mysql80-community/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
ui_id = mysql80-community/x86_64
ui_repoid_vars = releasever,
basearch
username =
[root@iZbp117qjre4vusjrgw7ftZ /]# sudo yum-config-manager --enable mysql57-community
Loaded plugins: fastestmirror, langpacks
========================================================================================================================== repo: mysql57-community ===========================================================================================================================
[mysql57-community]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/2.1903
baseurl = http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
cache = 0
cachedir = /var/cache/yum/x86_64/2.1903/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/2.1903/mysql57-community/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/2.1903/mysql57-community/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
hdrdir = /var/cache/yum/x86_64/2.1903/mysql57-community/headers
http_caching = all
includepkgs =
ip_resolve =
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid =
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink =
minrate = 0
mirrorlist =
mirrorlist_expire = 86400
name = MySQL 5.7 Community Server
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/2.1903/mysql57-community
pkgdir = /var/cache/yum/x86_64/2.1903/mysql57-community/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
ui_id = mysql57-community/x86_64
ui_repoid_vars = releasever,
basearch
username =
安装mysql
sudo yum install mysql-community-server
该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等。安装时间会比较长
[root@iZbp117qjre4vusjrgw7ftZ /]# sudo yum install mysql-community-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
mysql-connectors-community | 2.6 kB 00:00:00
mysql-tools-community | 2.6 kB 00:00:00
mysql57-community | 2.6 kB 00:00:00
mysql57-community/x86_64/primary_db | 247 kB 00:00:04
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.7.32-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.7.32-1.el7 for package: mysql-community-server-5.7.32-1.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.32-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.7.32-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.32-1.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.7.32-1.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.7.32-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================================================================================================
Installing:
mysql-community-server x86_64 5.7.32-1.el7 mysql57-community 173 M
Installing for dependencies:
mysql-community-client x86_64 5.7.32-1.el7 mysql57-community 25 M
mysql-community-common x86_64 5.7.32-1.el7 mysql57-community 308 k
mysql-community-libs x86_64 5.7.32-1.el7 mysql57-community 2.3 M
Transaction Summary
==============================================================================================================================================================================================================================================================================
Install 1 Package (+3 Dependent packages)
Total download size: 201 M
Installed size: 875 M
Is this ok [y/d/N]: y
Downloading packages:
(1/4): mysql-community-common-5.7.32-1.el7.x86_64.rpm | 308 kB 00:00:05
(2/4): mysql-community-libs-5.7.32-1.el7.x86_64.rpm | 2.3 MB 00:00:02
(3/4): mysql-community-client-5.7.32-1.el7.x86_64.rpm | 25 MB 00:00:14
(4/4): mysql-community-server-5.7.32-1.el7.x86_64.rpm | 173 MB 00:01:34
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 2.0 MB/s | 201 MB 00:01:41
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : mysql-community-common-5.7.32-1.el7.x86_64 1/4
Installing : mysql-community-libs-5.7.32-1.el7.x86_64 2/4
Installing : mysql-community-client-5.7.32-1.el7.x86_64 3/4
Installing : mysql-community-server-5.7.32-1.el7.x86_64 4/4
Verifying : mysql-community-server-5.7.32-1.el7.x86_64 1/4
Verifying : mysql-community-common-5.7.32-1.el7.x86_64 2/4
Verifying : mysql-community-libs-5.7.32-1.el7.x86_64 3/4
Verifying : mysql-community-client-5.7.32-1.el7.x86_64 4/4
Installed:
mysql-community-server.x86_64 0:5.7.32-1.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.7.32-1.el7 mysql-community-common.x86_64 0:5.7.32-1.el7 mysql-community-libs.x86_64 0:5.7.32-1.el7
Complete!
启动Mysql
systemctl start mysqld.service
systemctl status mysqld.service
[root@iZbp117qjre4vusjrgw7ftZ /]# systemctl start mysqld.service
[root@iZbp117qjre4vusjrgw7ftZ /]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2021-01-05 09:03:45 CST; 5s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 9312 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 9258 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 9315 (mysqld)
CGroup: /system.slice/mysqld.service
└─9315 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Jan 05 09:03:40 iZbp117qjre4vusjrgw7ftZ systemd[1]: Starting MySQL Server...
Jan 05 09:03:45 iZbp117qjre4vusjrgw7ftZ systemd[1]: Started MySQL Server.
停止Mysql
systemctl stop mysqld.service
重启Mysql
systemctl restart mysqld.service
初始密码
MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中:
[root@iZbp117qjre4vusjrgw7ftZ /]# grep 'temporary password' /var/log/mysqld.log
2021-01-05T01:03:43.082918Z 1 [Note] A temporary password is generated for root@localhost: 03X5g%iO6ul.
修改默认密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Su#2021Gjj';
Query OK, 0 rows affected (0.00 sec)
设置开机启动
[root@iZbp117qjre4vusjrgw7ftZ /]# systemctl enable mysqld
[root@iZbp117qjre4vusjrgw7ftZ /]# systemctl daemon-reload
开放远程连接
mysql>use mysql;
msyql>update user set user.Host='%' where user.User='root';
mysql>flush privileges;