首先要查看一下CentOS 7.9系统是否已安装mysql历史版本,可使用如下命令:

rpm -qa|grep mysql
rpm -qa|grep mariadb

如存在mysql历史版本可用以下命令,依次进行卸载。

yum -y remove  mysql安装包名称
yum -y remove  mariadb安装包名称

由于CentOS7默认yum安装数据库是mariadb,因此需要通过repo源进行安装mysql8.0最新版本。

[root@hecs-164387 software]# wget  https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@hecs-164387 software]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm

防止yum源缓存导致不生效,更新缓存

[root@hecs-164387 software]# yum clean all
[root@hecs-164387 software]# yum makecache

安装Mysql8.0数据库

[root@hecs-164387 software]# yum install mysql-community-server mysql-community
[root@hecs-164387 software]# mysql -V   //查看当前安装mysql版本

安装报错信息如下:
image

warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-server-8.0.31-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 8.0 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-8.0.31-1.el7.x86_64
 GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

解决方法,导入KEY

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

查看并修改初始密码:

[root@hecs-164387 /]# systemctl start mysqld	//启动mysql服务
[root@hecs-164387 /]# grep "password" /var/log/mysqld.log	//通过log查看初始密码
2022-11-26T12:55:04.513824Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: Q;I<E_R<v1fM	/Q;I<E_R<v1fM此为mysql初始密码
[root@hecs-164387 /]# mysql -u root -p
Enter password: 						//输入初始密码Q;I<E_R<v1fM
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.31

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> 

mysql> alter user 'root'@'localhost' identified by 'Password@123..';		//重置mysql密码为:Password@123..
Query OK, 0 rows affected (0.00 sec)

mysql> update mysql.user set host='%' where host='localhost';  		//root用户可使用任意IP登录数据库
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges; 		//刷新权限
Query OK, 0 rows affected (0.00 sec)

至此Mysql数据库已安装配置完成,可以开始使用了。

上一篇 下一篇