1、查看系统版本:
# cat /etc/centos-release
2、删除CentOS 7.9内置的MariaDB相关组件:
# rpm -qa | grep -i mariadb
# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
说明:
(1)如果之前安装过MySQL,先卸载:
# rpm -qa | grep -i mysql
(2)如果存在/etc/my.cnf配置文件,先删除:
# rm -rf /etc/my.cnf
3、安装mysql57-community-release-el7.rpm:
# rpm -ivh http://repo.mysql.com/mysql57-community-release-el7.rpm
# ls /etc/yum.repos.d | grep mysql
备注:安装后会在/etc/yum.repos.d目录生成mysql-community.repo和mysql-community-source.repo
4、安装MySQL:
# yum list | grep mysql-community
# rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
======================================================================================
说明:不执行上述import操作,会有如下提示,无法安装MySQL,因为MySQL GPG密钥过期导致,解决方案参考:https://bugs.mysql.com/bug.php?id=106188
warning: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-libs-compat-5.7.38-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-libs-compat-5.7.38-1.el7.x86_64
GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
======================================================================================
# yum -y install mysql-community-client mysql-community-common mysql-community-devel mysql-community-libs mysql-community-libs-compat mysql-community-server mysql-community-test
5、初始化MySQL:
# mysqld --initialize --user=mysql --datadir=/var/lib/mysql
备注:初始化之前确保/var/lib/mysql目录为空
6、修改/etc/my.cnf配置文件:
# mv /etc/my.cnf /etc/my.cnf.bak
# vim /etc/my.cnf
[mysqld]
port=3306
socket=/var/lib/mysql/mysql.sock
datadir=/var/lib/mysql
pid-file=/var/run/mysqld/mysqld.pid
log-error=/var/log/mysqld.log
lower_case_table_names=1
character_set_server=utf8mb4
collation_server=utf8mb4_general_ci
innodb_file_per_table=1
skip_name_resolve=1
slow_query_log=1
slow_query_log_file=mysql-slow.log
symbolic-links=0
explicit_defaults_for_timestamp=1
server_id=1
sync_binlog=1
innodb_flush_log_at_trx_commit=1
log_bin=mysql-bin
log_bin_index=mysql-bin.index
binlog_format=row
7、启动MySQL:
# systemctl start mysqld
# systemctl status mysqld
# ps aux | grep mysqld
# ss -tunlp | grep mysqld
# tail -100 /var/log/mysqld.log
8、配置MySQL开机自启:
# systemctl enable mysqld
9、查看root@localhost用户初始密码:
# grep password /var/log/mysqld.log
10、配置MySQL安全向导:
# mysql_secure_installation
11、授权root用户远程登录:
# mysql -uroot -p
mysql> create user root@'192.168.0.%' identified by '12345678';
mysql> grant all on *.* to root@'192.168.0.%';
mysql> flush privileges;
12、使用Navicat Premium连接MySQL: