00. 目录
01. MySQL开启远程登录权限
在Ubuntu下MySQL缺省是只允许本地访问的,如果需要第三方工具连接MySQL则需要进行配置。
1.1 查看远程登录权限
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 修改表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 刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
1.4 修改配置文件
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 重启mysql服务
deng@local:~$ sudo systemctl restart mysql.service
deng@local:~$
02. Navicat12连接MySQL8数据库
2.1 选择连接中的MySQL
2.2 新建连接,输入连接信息,点击连接测试
2.3 连接成功,点击确定
2.4 点击确定,连接MySQL数据库
03. 修改MySQL密码(选做)
3.1 修改密码的时候出现以下错误
mysql> alter user 'deng'@'%' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>
3.2 查看 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 设置密码策略
# 设置密码策略为低等级
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 查看密码策略等级
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 修改密码
mysql> alter user 'root'@'%' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>
3.6 刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql>
3.7 重新登录MySQL
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. 添加用户(选做)
4.1 登录MySQL数据库
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 授权(具有所有数据库所有的权限)
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. 讨论
5.1 出现caching_sha2_password错误
该错误的原因是在MySQL8之前版本中加密规则是mysql_native_password,而在MySQL8以后的加密规则为caching_sha2_password。
解决此问题有两种方法,一种是更新navicat驱动来解决此问题,一种是将mysql用户登录的加密规则修改为mysql_native_password。此处采用第二种方式。
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错误
原因分析: 没有开启远程连接
解决办法:
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:~$