1.連接數據庫
1 [root@localhost ~]# mysql -uroot -p 2 Enter password: 3 Welcome to the MySQL monitor. Commands end with ; or \g. 4 Your MySQL connection id is 17 5 Server version: 8.0.17 MySQL Community Server - GPL 6 7 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 8 9 Oracle is a registered trademark of Oracle Corporation and/or its 10 affiliates. Other names may be trademarks of their respective 11 owners. 12 13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 15 mysql>
2.在mysql8版本中更改用戶密碼需要加入with mysql_native_password
1 mysql> alter user 'root'@'localhost' identified with mysql_native_password by '***
'; 2 Query OK, 0 rows affected (0.04 sec)
3.修改root用戶的host為‘%’
1 mysql> show databases; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | mysql | 7 | performance_schema | 8 | sys | 9 +--------------------+ 10 4 rows in set (0.01 sec) 11 12 mysql> use mysql; 13 Reading table information for completion of table and column names 14 You can turn off this feature to get a quicker startup with -A 15 16 Database changed 17 18 mysql> select user,host from user; 19 +------------------+-----------+ 20 | user | host | 21 +------------------+-----------+ 22 | mysql.infoschema | localhost | 23 | mysql.session | localhost | 24 | mysql.sys | localhost | 25 | root | localhost | 26 +------------------+-----------+ 27 4 rows in set (0.00 sec) 28 29 mysql> update user set host = '%' where user = 'root'; 30 Query OK, 1 row affected (0.00 sec) 31 Rows matched: 1 Changed: 1 Warnings: 0 32 33 mysql> flush privileges; 34 Query OK, 0 rows affected (0.00 sec)
再查看root用戶的host
1 mysql> select user,host from user; 2 +------------------+-----------+ 3 | user | host | 4 +------------------+-----------+ 5 | root | % | 6 | mysql.infoschema | localhost | 7 | mysql.session | localhost | 8 | mysql.sys | localhost | 9 +------------------+-----------+ 10 4 rows in set (0.01 sec)
至此,root用戶的遠程訪問已開啟。
4.測試下navicat連接
連接成功!
常見問題解決
1.解決sql_mode=only_full_group_by問題
[Err] 1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mingbyte.u.USER_ID' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
連接數據庫,通過 select version(), @@sql_mode; 查看mode
1 [root@localhost ~]# mysql -uroot -p 2 Enter password: 3 Welcome to the MySQL monitor. Commands end with ; or \g. 4 Your MySQL connection id is 89 5 Server version: 8.0.17 MySQL Community Server - GPL 6 7 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 8 9 Oracle is a registered trademark of Oracle Corporation and/or its 10 affiliates. Other names may be trademarks of their respective 11 owners. 12 13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 14 15 mysql> select version(), @@sql_mode; 16 +-----------+-----------------------------------------------------------------------------------------------------------------------+ 17 | version() | @@sql_mode | 18 +-----------+-----------------------------------------------------------------------------------------------------------------------+ 19 | 8.0.17 | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION | 20 +-----------+-----------------------------------------------------------------------------------------------------------------------+ 21 1 row in set (0.00 sec) 22 23 mysql>
發現上文中查看的mode中有ONLY_FULL_GROUP_BY。將其刪除
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
修改mysql的配置文件,重啟依舊生效
- vi /etc/my.cnf
將上述sql_mode插入[mysqld]下方,保存,重啟數據庫,問題解決。
或者
1 mysql> set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; 2 Query OK, 0 rows affected (0.01 sec) 3 4 mysql> set session sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; 5 Query OK, 0 rows affected (0.00 sec)
問題解決。