1. 删除已安装的MySQL
1.1 检查MariaDB
# rpm -qa|grep mariadb
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
1.2 删除mariadb
如果上面检查没有,直接跳过当前步骤
#rpm -e --nodeps mariadb-server
# rpm -e --nodeps mariadb
# rpm -e --nodeps mariadb-libs
1.3 检查MySQL
# rpm -qa|grep mysql
mysql80-community-release-el7-3.noarch
mysql-community-client-5.7.34-1.el7.x86_64
mysql-community-libs-5.7.34-1.el7.x86_64
mysql-community-server-5.7.34-1.el7.x86_64
mysql-community-common-5.7.34-1.el7.x86_64
mysql-community-libs-compat-5.7.34-1.el7.x86_64
1.4 删除MySQL
#rpm -e --nodeps mysql80-community
#rpm -e --nodeps mysql-community-client
#rpm -e --nodeps mysql-community-libs
#rpm -e --nodeps mysql-community-server
#rpm -e --nodeps mysql-community-common
#rpm -e --nodeps mysql-community-libs-compat
2. 添加MySQL Yum 源
2.1 查看系统版本:
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
2.2 下载MySQL源
去MySQL官网下载与系统匹配的版本。CentOS 7下载mysql80-community-release-el7-3.noarch.rpm
# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
2.3 安装MySQL源
# rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
2.4 检查是否安装成功
执行成功后会在/etc/yum.repos.d/
目录下生成两个repo文件mysql-community.repo
及 mysql-community-source.repo
并且通过yum repolist
可以看到mysql相关资源
# yum repolist enabled | grep "mysql.*-community.*"
!mysql-connectors-community/x86_64 MySQL Connectors Community 203
!mysql-tools-community/x86_64 MySQL Tools Community 129
!mysql57-community/x86_64 MySQL 5.7 Community Server 504
3. 选择MySQL版本
使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本,例如通过上面的MySQL源进行安装的话,默安装会选择MySQL 8.0版本,如果就是想要安装该版本,可以直接跳过此步骤,如果不是,比如我这里希望安装MySQL5.7版本,就需要“切换一下版本”:
3.1 查看当前MySQL Yum Repository中所有MySQL版本
# yum repolist all | grep mysql
3.2 切换版本
# sudo yum-config-manager --disable mysql80-community
# sudo yum-config-manager --enable mysql57-community
4. 安装MySQL
# yum install mysql-community-server
5. 启停MySQL
# systemctl start mysqld.service #启动
# systemctl stop mysqld.service #停止
# systemctl restart mysqld.service #重启
# systemctl status mysqld.service #查看状态
6. 修改密码
6.1 初始密码
MySQL第一次启动后会创建超级管理员账号root@localhost,初始密码存储在日志文件中
# grep 'temporary password' /var/log/mysqld.log
6.2 修改默认密码
使用初始密码登录MySQL修改
# mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
出现上面的提示是因为密码太简单了,解决方法如下:
- 使用复杂密码,MySQL默认的密码策略是要包含数字、字母及特殊字符;
- 如果只是测试用,不想用那么复杂的密码,可以修改默认策略,即validate_password_policy(以及validate_password_length等相关参数),使其支持简单密码的设定,具体方法可以自行百度;
- 修改配置文件/etc/my.cnf,添加validate_password=OFF,保存并重启MySQL
7. 允许root远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
8. 设置编码为utf8
8.1 查看编码
mysql> SHOW VARIABLES LIKE 'character%';
8.2 设置编码
编辑/etc/my.cnf,[mysqld]节点增加以下代码:
[mysqld]
character_set_server=utf8
init-connect='SET NAMES utf8'
9. 设置开机启动
# systemctl enable mysqld
# systemctl daemon-reload