执行以下命令,查看系统中是否已安装 MySQL
rpm -qa | grep -i mysql
如果返回值为空,则证明没安装
1.下载mysql源安装包
$ wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2.安装mysql源
$ yum localinstall mysql57-community-release-el7-8.noarch.rpm
3.检查mysql源是否安装成功
$ yum repolist enabled | grep "mysql.*-community.*"
4.修改yum源 【可跳过】
$ vim /etc/yum.repos.d/mysql-community.repo
改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。
备注:enabled=1表示即将要安装的mysql版本,这个文件也可以不修改,默认安装mysql最高版本
5.安装MySQL
这一步才是真正安装mysql
$ yum install mysql-community-server
6.启动MySQL服务并设置开机启动
$ systemctl start mysqld
$ systemctl enable mysqld
$ systemctl daemon-reload
7.端口开放
$ firewall-cmd --zone=public --add-port=3306/tcp --permanent
$ firewall-cmd --reload
8.修改root本地登录密码
1)查看mysql密码,root@localhost:
后面的就是密码
$ grep 'temporary password' /var/log/mysqld.log
[root@VM_0_5_centos ~]# grep 'temporary password' /var/log/mysqld.log
2019-11-30T08:22:59.518317Z 1 [Note] A temporary password is generated for root@localhost: h/IX?ruGr4Hk
2)连接mysql
$ mysql -uroot -p
8.3)修改密码【注意:后面的分号一定要跟上】
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新的密码';
或者:
mysql> set password for 'root'@'localhost'=password('新的密码');
如果密码太简单会出现下面的提示
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决办法
1、查看 mysql 初始的密码策略
SHOW VARIABLES LIKE 'validate_password%';
2、修改设置密码的验证强度等级
set global validate_password_policy=LOW;
3、修改密码,最短长度限制为6
set global validate_password_length=6;
4、重复8.3的操作
9.添加远程登录用户
mysql> GRANT ALL PRIVILEGES ON *.* TO 'caoxiaobo'@'%' IDENTIFIED BY 'Caoxiaobo0917!' WITH GRANT OPTION;