1 查看是否已安装mysql,如果已安装先卸载
[work@lsdBKusFRn mysql]$ rpm -qa | grep mysql
mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64
mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64
mysql57-community-release-el7-10.noarch
可以看到之前有安装过mysql,先将其从系统卸载
[root@lsdBKusFRn ~]# yum remove mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Removing:
mysql-common x86_64 8.0.26-1.module_el8.4.0+915+de215114 @AppStream 460 k
Removing dependent packages:
mysql x86_64 8.0.26-1.module_el8.4.0+915+de215114 @AppStream 63 M
Removing unused dependencies:
mariadb-connector-c-config noarch 3.1.11-2.el8_3 @AppStream 497
Transaction Summary
========================================================================================================================================================================
Remove 3 Packages
Freed space: 63 M
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Erasing : mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 1/3
Erasing : mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64 2/3
Erasing : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 3/3
Running scriptlet: mariadb-connector-c-config-3.1.11-2.el8_3.noarch 3/3
Verifying : mariadb-connector-c-config-3.1.11-2.el8_3.noarch 1/3
Verifying : mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 2/3
Verifying : mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64 3/3
Removed:
mariadb-connector-c-config-3.1.11-2.el8_3.noarch mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64 mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64
Complete!
[root@lsdBKusFRn ~]# yum remove mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64
No match for argument: mysql-8.0.26-1.module_el8.4.0+915+de215114.x86_64
No packages marked for removal.
Dependencies resolved.
Nothing to do.
Complete!
[root@lsdBKusFRn ~]# yum remove mysql57-community-release-el7-10.noarch
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Removing:
mysql57-community-release noarch el7-10 @@commandline 30 k
Transaction Summary
========================================================================================================================================================================
Remove 1 Package
Freed space: 30 k
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Erasing : mysql57-community-release-el7-10.noarch 1/1
Verifying : mysql57-community-release-el7-10.noarch 1/1
Removed:
mysql57-community-release-el7-10.noarch
Complete!
执行完卸载命令,我们再确认一下是否已从系统卸载完成
[root@lsdBKusFRn ~]# rpm -qa|grep mysql
没有任何输出,表示已经卸载完成
2 下载官方mysql安装包
按照需求从官网下载相应版本的mysql安装包
[root@lsdBKusFRn ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
--2022-05-28 18:12:48-- https://dev.mysql.com/get/mysql57-community-release-el7-11.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//mysql57-community-release-el7-11.noarch.rpm [following]
--2022-05-28 18:12:49-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.58.116.230
Connecting to repo.mysql.com (repo.mysql.com)|23.58.116.230|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 25680 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql57-community-release-el7-11.noarch.rpm’
mysql57-community-release-el7-11.noarch.r 100%[=====================================================================================>] 25.08K 130KB/s in 0.2s
2022-05-28 18:12:50 (130 KB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680]
3 安装mysql
执行安装mysql源命令
[root@lsdBKusFRn ~]# yum -y localinstall mysql57-community-release-el7-11.noarch.rpm
Last metadata expiration check: 0:19:31 ago on Sat 28 May 2022 05:53:32 PM CST.
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Installing:
mysql57-community-release noarch el7-11 @commandline 25 k
Transaction Summary
========================================================================================================================================================================
Install 1 Package
Total size: 25 k
Installed size: 31 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql57-community-release-el7-11.noarch 1/1
Verifying : mysql57-community-release-el7-11.noarch 1/1
Installed:
mysql57-community-release-el7-11.noarch
Complete!
再安装mysql服务器
[root@lsdBKusFRn ~]# yum -y install mysql-community-server
Last metadata expiration check: 0:19:36 ago on Sat 28 May 2022 05:53:34 PM CST.
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Installing:
mysql-community-server x86_64 5.7.38-1.el7 mysql57-community 178 M
Installing dependencies:
mysql-community-client x86_64 5.7.38-1.el7 mysql57-community 28 M
mysql-community-common x86_64 5.7.38-1.el7 mysql57-community 311 k
mysql-community-libs x86_64 5.7.38-1.el7 mysql57-community 2.6 M
Transaction Summary
========================================================================================================================================================================
Install 4 Packages
Total size: 208 M
Installed size: 890 M
Downloading Packages:
[SKIPPED] mysql-community-client-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-common-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-libs-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-server-5.7.38-1.el7.x86_64.rpm: Already downloaded
MySQL 5.7 Community Server 27 MB/s | 27 kB 00:00
GPG key at file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql (0x5072E1F5) is already installed
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-client-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-common-5.7.38-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-common-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-libs-5.7.38-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-libs-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Public key for mysql-community-server-5.7.38-1.el7.x86_64.rpm is not installed. Failing package is: mysql-community-server-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
The downloaded packages were saved in cache until the next successful transaction.
You can remove cached packages by executing 'yum clean packages'.
Error: GPG check FAILED
注意到这一步有可能会报错:Error: GPG check FAILED,解决办法如下:添加–nogpgcheck参数
[root@lsdBKusFRn ~]# yum -y install mysql-community-server –nogpgcheck
Last metadata expiration check: 0:20:06 ago on Sat 28 May 2022 05:53:34 PM CST.
No match for argument: –nogpgcheck
Error: Unable to find a match: –nogpgcheck
[root@lsdBKusFRn ~]# yum install mysql-community-server --nogpgcheck
Last metadata expiration check: 0:20:17 ago on Sat 28 May 2022 05:53:34 PM CST.
Dependencies resolved.
========================================================================================================================================================================
Package Architecture Version Repository Size
========================================================================================================================================================================
Installing:
mysql-community-server x86_64 5.7.38-1.el7 mysql57-community 178 M
Installing dependencies:
mysql-community-client x86_64 5.7.38-1.el7 mysql57-community 28 M
mysql-community-common x86_64 5.7.38-1.el7 mysql57-community 311 k
mysql-community-libs x86_64 5.7.38-1.el7 mysql57-community 2.6 M
Transaction Summary
========================================================================================================================================================================
Install 4 Packages
Total size: 208 M
Installed size: 890 M
Is this ok [y/N]: y
Downloading Packages:
[SKIPPED] mysql-community-client-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-common-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-libs-5.7.38-1.el7.x86_64.rpm: Already downloaded
[SKIPPED] mysql-community-server-5.7.38-1.el7.x86_64.rpm: Already downloaded
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mysql-community-common-5.7.38-1.el7.x86_64 1/4
Installing : mysql-community-libs-5.7.38-1.el7.x86_64 2/4
Running scriptlet: mysql-community-libs-5.7.38-1.el7.x86_64 2/4
Installing : mysql-community-client-5.7.38-1.el7.x86_64 3/4
Running scriptlet: mysql-community-server-5.7.38-1.el7.x86_64 4/4
Installing : mysql-community-server-5.7.38-1.el7.x86_64 4/4
Running scriptlet: mysql-community-server-5.7.38-1.el7.x86_64 4/4
[/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.
Verifying : mysql-community-client-5.7.38-1.el7.x86_64 1/4
Verifying : mysql-community-common-5.7.38-1.el7.x86_64 2/4
Verifying : mysql-community-libs-5.7.38-1.el7.x86_64 3/4
Verifying : mysql-community-server-5.7.38-1.el7.x86_64 4/4
Installed:
mysql-community-client-5.7.38-1.el7.x86_64 mysql-community-common-5.7.38-1.el7.x86_64 mysql-community-libs-5.7.38-1.el7.x86_64
mysql-community-server-5.7.38-1.el7.x86_64
Complete!
4 启动mysql
执行启动mysql命令
[root@lsdBKusFRn ~]# systemctl start mysqld.service
没有报错,没有输出,表示启动成功,执行以下命令验证是否已启动
[root@lsdBKusFRn ~]# service mysqld status
Redirecting to /bin/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-05-28 18:31:01 CST; 1min 15s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 234488 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 234416 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 234491 (mysqld)
Tasks: 28 (limit: 6010)
Memory: 362.4M
CGroup: /system.slice/mysqld.service
└─234491 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
May 28 18:30:19 lsdBKusFRn systemd[1]: Starting MySQL Server...
May 28 18:31:01 lsdBKusFRn systemd[1]: Started MySQL Server.
当有以上输出时,表示mysql已成功启动
5 设置mysql登录密码
系统默认为root生成一个临时密码,查找方式如下:
[root@lsdBKusFRn ~]# grep 'temporary password' /var/log/mysqld.log
2022-05-28T10:30:58.944136Z 1 [Note] A temporary password is generated for root@localhost: PBtw&U5%7u>t
得到root密码是:PBtw&U5%7u>t,用该密码登录
[root@lsdBKusFRn ~]# mysql -hlocalhost -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.38
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
如果需要修改root密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'QwerQwer@@@750';
Query OK, 0 rows affected (0.00 sec)
至此mysql安装完成,且可以正常登录