添加MySQL Yum库
# wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
# rpm -ivh mysql80-community-release-el7-1.noarch.rpm
选择要安装的MySQL版本
# vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0 // 将这里的0改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1 //将这里的1改为0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
enabled=0为禁用对应版本的YUM库,enabled=1为启用,默认8.0的配置为enabled=1,安装5.7的话,就把8.0的enabled=1修改为enabled=0,然后把5.7的enabled=0修改为enabled=1,修改完成,:wq保存
安装MySQL并启动
# yum install mysql-community-server
# service mysqld start
查看MySQL初始密码
# sudo grep 'temporary password' /var/log/mysqld.log
如果查询为空则无密码,直接回车即可
修改MySQL初始密码
# mysql -u root -p
> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
给root 权限开启 远程登录
> use mysql;
> select user,host from user;
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
为数据库创建单独的用户并给他单独数据库的所有权限
> create user 'test'@'%' identified by 'CKNiNXZgLk4H2kqYS';
> grant all privileges ON test.* TO 'test'@'%';
> ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY 'CKNiNXZgLk4H2kqYS';
推荐
更改默认3306端口
# vi /etc/my.cnf
添加 port= 端口号 即可
# systemctl restart mysqld
设置MySQL数据库名不区分大小写
# vi /etc/my.cnf
在mysqld里加上
lower_case_table_names=1
# systemctl restart mysqld
ERROR
ERROR 3009 (HY000): Column count of mysql.user is wrong. Expected 45, found 42. Created with MySQL 50564, now running 50729. Please use mysql_upgrade to fix this error.
# mysql_upgrade -u root -p
# mysql -uroot -p
密码为空直接回车
ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
# vi /etc/my.cnf
skip-grant-tables // 添加
# service mysqld restart
# mysql -u root -p
> set password for 'root'@'localhost' = password('yourpasswd');
> quit
# vi /etc/my.cnf
skip-grant-tables // 删除
# service mysqld restart
[Err] 1682 - Native table ‘performance_schema’.‘session_status’ has the wron
# mysql_upgrade -u root -p
> set @@global.show_compatibility_56=ON;
注:从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看 mysql 初始的密码策略
SHOW VARIABLES LIKE 'validate_password%';
设置密码的验证强度等级为LOW
set global validate_password_policy=LOW;
设置密码长度为6
set global validate_password_length=6;