删除已安装的MySQL
检查MariaDB
shell> 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
删除mariadb
shell> rpm -e --nodeps mariadb-server
shell> rpm -e --nodeps mariadb
shell> rpm -e --nodeps mariadb-libs
检查MySQL
shell> rpm -qa|grep mysql
删除MySQL
shell> rpm -e --nodeps xxx
添加MySQL Yum Repository
下载MySQL源
官网地址:https://dev.mysql.com/downloads/repo/yum/
查看系统版本:
shell> cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
选择对应的版本进行下载
shell> wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
安装MySQL源
shell> sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm
检查是否安装成功
执行成功后会在/etc/yum.repos.d/
目录下生成两个repo
文件mysql-community.repo
及 mysql-community-source.repo
并且通过yum repolist
可以看到mysql相关资源
shell> yum repolist enabled | grep "mysql.*-community.*"
!mysql-connectors-community/x86_64 MySQL Connectors Community 108
!mysql-tools-community/x86_64 MySQL Tools Community 90
!mysql80-community/x86_64 MySQL 8.0 Community Server 113
选择MySQL版本
使用MySQL Yum Repository安装MySQL,默认会选择当前最新的稳定版本,例如通过上面的MySQL源进行安装的话,默安装会选择MySQL 8.0版本,如果就是想要安装该版本,可以直接跳过此步骤,如果不是,比如我这里希望安装MySQL5.7版本,就需要“切换一下版本”:
查看当前MySQL Yum Repository中所有MySQL版本(每个版本在不同的子仓库中)
shell> yum repolist all | grep mysql
切换版本
shell> sudo yum-config-manager --disable mysql80-community
shell> sudo yum-config-manager --enable mysql57-community
如果显示-bash: yum-config-manager: command not found
,则使用yum -y install yum-utils
安装yum管理包
除了使用yum-config-manager
之外,还可以直接编辑/etc/yum.repos.d/mysql-community.repo
文件enabled=0禁用
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
enabled=1启用
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
检查当前启用的MySQL仓库
shell> yum repolist enabled | grep mysql
安装MySQL
shell> sudo yum install mysql-community-server
该命令会安装MySQL服务器 (mysql-community-server) 及其所需的依赖、相关组件,包括mysql-community-client、mysql-community-common、mysql-community-libs等
如果带宽不够,这个步骤时间会比较长,请耐心等待~
启动MySQL
启动
shell> sudo systemctl start mysqld.service
查看状态
shell> sudo systemctl status mysqld.service
停止
shell> sudo systemctl stop mysqld.service
重启
shell> sudo service mysqld restart
修改密码
初始密码
MySQL第一次启动后会创建超级管理员账号root@localhost
,初始密码存储在日志文件中:
shell> sudo grep 'temporary password' /var/log/mysqld.log
修改默认密码
shell> 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
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
允许root远程访问
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;
设置编码为utf8
查看编码
mysql> SHOW VARIABLES LIKE 'character%';
设置编码
编辑/etc/my.cnf,[mysqld]节点增加以下代码:
[mysqld]
character_set_server=utf8
init-connect='SET NAMES utf8'
设置开机启动
shell> systemctl enable mysqld
shell> systemctl daemon-reload