1.第一种

~~ [root@centos7 ~]#mysqladmin -uroot -p123456 password aaaaaa Warning: Using a password on the command line interface can be insecure. [root@centos7 ~]#mysql -uroot -paaaaaa Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.6.51 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.
~~(root@localhost) [(none)]> ~~


2.第二种 (mysql5.6 的安全加固方式)

[root@centos7 ~]#mysql_secure_installation ~~
~~NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, and
you haven’t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): —-这里是如果之前是没有密码的,直接回车,反之输入密码
OK, successfully used password, moving on…

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y —-这里打y 说明确认设置
New password: ——输入新密码
Re-enter new password: —-确认新密码
Password updated successfully!
Reloading privilege tables..
… Success!

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. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y —移除匿名登录的方式
… Success!

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? [Y/n] y ——-禁止远程登录
… Success!

By default, MySQL comes with a database named ‘test’ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y ——移除test数据库

  • Dropping test database…
    ERROR 1008 (HY000) at line 1: Can’t drop database ‘test’; database doesn’t exist
    … Failed! Not critical, keep moving…
  • Removing privileges on test database…
    … Success!


Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
… Success!

All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!
~~Cleaning up…
~~

修改账号和权限,应该刷新权限才会生效。
~~flush privileges ~~


新版本的mysql, user表里面装的password字段已经被变更成其他的字段了。
image.png
image.png
mysql> select host,user from user; +—————-+—————————+
| host | user |
+—————-+—————————+
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+—————-+—————————+
4 rows in set (0.00 sec)

mysql> select host,user,authentication_string from user;
+—————-+—————————+—————————————————————-+
| host | user | authentication_string |
+—————-+—————————+—————————————————————-+
| localhost | root | |
| localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | *2B84763B9549DDEFC7EA6E41B621165883C02DE7 |
+—————-+—————————+—————————————————————-+
4 rows in set (0.00 sec)


mysql_secure_installation:
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: #不启用密码增强插件

~~New password: ~~

Re-enter new password: ~~
Sorry, passwords do not match.~~

~~New password: ~~

Re-enter new password: ~~
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. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
#输入了新密码:111111~~

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.

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) : no^H^H

~~ … skipping.
By default, MySQL comes with a database named ‘test’ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
#禁止用户远程登录吗? 不禁止,方便我们后面做远程数据库查看~~

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
~~ - Dropping test database…
Success.~~

~~ - Removing privileges on test database…
Success.~~

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
#删除测试数据库吗? 是的!测试数据库没用

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
Success.

All done! ~~
#重新加载权限表,设置完毕。我们再试试我们的mysql。~~

3.第三种

新版本的mysql, user表里面装的password字段已经被变更成其他的字段了。
image.png
image.png
mysql> select host,user from user; +—————-+—————————+
| host | user |
+—————-+—————————+
| localhost | debian-sys-maint |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+—————-+—————————+
4 rows in set (0.00 sec)

mysql> select host,user,authentication_string from user;
+—————-+—————————+—————————————————————-+
| host | user | authentication_string |
+—————-+—————————+—————————————————————-+
| localhost | root | |
| localhost | mysql.session | THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | mysql.sys |
THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
| localhost | debian-sys-maint | *2B84763B9549DDEFC7EA6E41B621165883C02DE7 |
+—————-+—————————+—————————————————————-+
4 rows in set (0.00 sec)

直接去数据库里修改localhost《—-》root这行的密码:

update user set authentication_string=PASSWORD(‘111111’) , HOST=’%’ where host=’localhost’ and user=’root’;

刷新权限:
flush privileges;

plugin=’mysql_native_password’