00. Table of Contents
Article directory
01. MySQL enables remote login permissions
Under Ubuntu, MySQL only allows local access by default. If you need a third-party tool to connect to MySQL, you need to configure it.
1.1 Check remote login permissions
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user, host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
5 rows in set (0.01 sec)
mysql>
1.2 Modify table user
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql>
1.3 Refresh permissions
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
1.4 Modify configuration file
deng@local:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 注释以下两行
31 #bind-address = 127.0.0.1
32 #mysqlx-bind-address = 127.0.0.1
1.5 Restart the mysql service
deng@local:~$ sudo systemctl restart mysql.service
deng@local:~$
02. Navicat12 connects to MySQL8 database
2.1 Select MySQL in the connection
2.2 Create a new connection, enter the connection information, and click Connection Test
2.3 The connection is successful, click OK
2.4 Click OK to connect to the MySQL database
03. Change MySQL password (optional)
3.1 The following error occurs when changing the password
mysql> alter user 'deng'@'%' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>
3.2 View the initial password policy of mysql:
mysql> alter user 'deng'@'%' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> SHOW VARIABLES LIKE 'validate_password%';
+-------------------------------------------------+--------+
| Variable_name | Value |
+-------------------------------------------------+--------+
| validate_password.changed_characters_percentage | 0 |
| 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 |
+-------------------------------------------------+--------+
8 rows in set (0.01 sec)
mysql>
#validate_password_length密码长度的最小值(这个值最小要是4)。
#validate_password_number_count 密码中数字的最小个数。
#validate_password_mixed_case_count大小写的最小个数。
#validate_password_special_char_count 特殊字符的最小个数。
#validate_password_dictionary_file 字典文件
3.3 Set password policy
# 设置密码策略为低等级
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
# 设置密码长度为6
mysql> set global validate_password.length=6;
Query OK, 0 rows affected (0.00 sec)
mysql>
3.4 View password policy level
mysql> SHOW VARIABLES LIKE 'validate_password%';
+-------------------------------------------------+-------+
| Variable_name | Value |
+-------------------------------------------------+-------+
| validate_password.changed_characters_percentage | 0 |
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 6 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+-------------------------------------------------+-------+
8 rows in set (0.00 sec)
mysql>
3.5 Change password
mysql> alter user 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
3.6 Refresh permissions
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
3.7 Log in to MySQL again
deng@local:~$ mysql -uroot -p123456
mysql: [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 16
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
mysql>
04. Add user (optional)
4.1 Log in to the MySQL database
deng@local:~$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
mysql>
4.2 Authorization (has all permissions for all databases)
mysql> CREATE USER 'deng'@'%' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.02 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'deng'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.10 sec)
mysql>
05. Discussion
5.1 A cache_sha2_password error occurs
The reason for this error is that the encryption rule in versions before MySQL8 is mysql_native_password, while the encryption rule in MySQL8 and later versions is caching_sha2_password.
There are two ways to solve this problem. One is to update the navicat driver to solve this problem, and the other is to modify the encryption rule for mysql user login to mysql_native_password. The second approach is used here.
mysql>
mysql> alter user 'deng'@'%' identified by '123456' password expire never;
Query OK, 0 rows affected (0.01 sec)
mysql> alter user 'deng'@'%' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
5.2 Unknown error occurs
Cause analysis: Remote connection is not enabled
Solution:
deng@local:~$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 注释31行和32行
14 [mysqld]
15 #
16 # * Basic Settings
17 #
18 user = mysql
19 # pid-file = /var/run/mysqld/mysqld.pid
20 # socket = /var/run/mysqld/mysqld.sock
21 # port = 3306
22 # datadir = /var/lib/mysql
23
24
25 # If MySQL is running as a replication slave, this should be
26 # changed. Ref https://dev.mysql.com/doc/refman/8.0/en/server-system-variabl es.html#sysvar_tmpdir
27 # tmpdir = /tmp
28 #
29 # Instead of skip-networking the default is now to listen only on
30 # localhost which is more compatible and is not less secure.
31 #bind-address = 127.0.0.1
32 #mysqlx-bind-address = 127.0.0.1
# 重新启动MySQL服务
deng@local:~$ sudo systemctl restart mysql.service
deng@local:~$