一、MySQL安装
1.下载并安装MySQL官方的Yum Repository
[root@hadoopstudy3 /]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
注:http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm是下载版本,可以去官网https://dev.mysql.com/downloads/repo/yum/下载需要的版本,然后上传到CentOS中
2.安装MySQL源
[root@hadoopstudy3 /]# yum -y install mysql-community-server
3.安装MySQL服务器
[root@hadoopstudy3 /]# yum -y install mysql-community-server
4.启动MySQL
[root@hadoopstudy3 /]# systemctl start mysqld.service
5.查看MySQL运行状态
[root@hadoopstudy3 /]# systemctl status mysqld.service
6.首次登录MySQL
我看很多人都是用grep "password" /var/log/mysqld.log命令抓取密码后,然后登录mysql。
登录mysql命令:mysql -u root -p 回车然后输入刚才抓取的密码,但我是重置密码然后登录。
二、重置MySQL密码
1.进入到my.cnf文件跳过MySQL的密码认证过程
[root@hadoopstudy3 /]# vim /etc/my.cnf
在[mysqld]下方加入一行:skip-grant-tables 如下图所示,然后保存退出
2.重启MySQL
[root@hadoopstudy3 /]# systemctl restart mysqld.service
3.登录MySQL
[root@hadoopstudy3 /]# mysql -u root -p
然后在输入密码时敲回车就进入mysql。
4.重置密码
①进入mysql数据库
mysql> use mysql
如下图所示
②更新密码
mysql> update user set authentication_string=passworD("password123") where user='root';
注:update user set authentication_string=passworD("你的密码") where user='root'; 此处我设置的密码为password123。
③执行并退出
mysql> flush privileges;
mysql> exit
5.进入/etc/my.cnf文件,将刚才添加的内容删去,保存退出
6.重启MySQL
[root@hadoopstudy3 /]# systemctl restart mysqld.service
完成重置密码后登录mysql:mysql -u root -p
然后输入你刚才设置的密码即可登录mysql
三、首次登录还需重置密码
在登陆mysql后使用命令例如:show databases;
会出现如下图提示:
此时需要重置你刚才设置的密码
1.设置密码
mysql> alter user 'root'@'localhost' identified by '123456';
若你的密码设置简单或未达到要求会出现错误如下图所示
这个其实与validate_password_policy的值有关。
validate_password_policy有以下取值:
Policy | Tests Performed |
0 or LOW | Length |
1 or MEDIUM | Length; numeric, lowercase/uppercase,and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase,and special characters; dictionary file |
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
修改方法:
①修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
②修改validate_password_length的值。
validate_password_length(密码长度)参数默认为8,将其修改为3
mysql> set global validate_password_length=3;
③修改你刚才输入的密码即可
mysql> alter user 'root'@'localhost' identified by '123456';
2.完成上述步骤,输入命令即可解决错误。