网上找到的这些关于这类的博客要么就是mysql版本过时了要么就完全没有用,浪费了我好多时间,于是我就把自己成功的经历分享给大家,希望能减少大家走弯路的时间。
很简单,一句话
sudo apt install mysql-server
修改密码我们得先进mysql,用管理员权限的话不论你输入什么样的密码都能成功。
sudo mysql -u root -p
quit;
之后对mysql密码的设置初始化
sudo mysql_secure_installation
之后的配置如图设置
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (选择N ,不会进行密码的强校验)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
(这里他会问你要不要修改密码,第一次选择y)
#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (选择N,不删除匿名用户)
#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N (选择N,允许root远程连接)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (选择N,不删除test数据库)
#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (选择Y,修改权限立即生效)
做完后再登录
sudo mysql -uroot -p
选择mysql database
use mysql;
尝试第一次设置密码,这一次设置有可能会报错Your password does not satisfy the current policy requirements,这是因为你密码的长度不符合长度要求,没报错则无需以下步骤。
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
若报错则执行以下步骤
SHOW VARIABLES LIKE 'validate_password%';
mysql会输出以下参数
+--------------------------------------+--------+
| Variable_name | Value |
+--------------------------------------+--------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 8 |//这个就是你要设置的密码长度
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | MEDIUM |
| validate_password.special_char_count | 1 |
+--------------------------------------+--------+
7 rows in set (0.01 sec)
之后就是根据你电脑的参数和这里的参数一个个对照着看,一般来说除了密码长度不一样之外都是一样的,若存在不一直则按如下方法修改。
1 mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
2 mysql> set global validate_password.special_char_count=0;
Query OK, 0 rows affected (0.00 sec)
3 mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)
4 mysql> set global validate_password.mixed_case_count=0;
Query OK, 0 rows affected (0.00 sec)
之后在来看一看参数修改成功没有
mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 6 |
| validate_password.mixed_case_count | 0 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 0 |
+--------------------------------------+-------+
7 rows in set (0.01 sec)
最后重新输入之前的修改密码的命令。(注意这次密码要和你上面修改的长度一致)
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
保存设置并退出
flush privileges;
quit;
最后我们来试验一下是否修改成功
mysql -u root -p //这里不要加sudo
输入密码,能进到mysql中不报错就是修改密码成功了。