代码改变世界

无法远程连接mysql,连接后也没有权限创建数据库

2017-08-01 15:01  yongchin  阅读(2497)  评论(0编辑  收藏  举报

问题现象:
无法远程连接mysql,连接后也没有权限创建数据库

问题原因:
MySql-Server 出于安全方面考虑只允许本机(localhost, 127.0.0.1)来连接访问. 这对于 Web-Server 与 MySql-Server 都在同一台服务器上的网站架构来说是没有问题的. 但随着网站流量的增加, 后期服务器架构可能会将 Web-Server 与 MySql-Server 分别放在独立的服务器上, 以便得到更大性能的提升, 此时 MySql-Server 就要修改成允许 Web-Server 进行远程连接.

解决方法:
登陆mysql,添加一个超级用户给所有IP即可

依次执行,用mysql root账号登陆mysql后依次执行

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

FLUSH PRIVILEGES;

 

 

 

mysql 8.0之后

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;不再适用,报错提示
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 123456 WITH GRANT OPTION' at line 1

 

mysql 8.0.11 解压版   
  
mysql> alter  user 'root'@'localhost' identified by '123456';  
Query OK, 0 rows affected (0.21 sec)  
  
mysql> flush privileges;  
Query OK, 0 rows affected (0.04 sec)  
  
--远程客户端连接  
mysql> update user set Host='%' where User='root';  
Query OK, 1 row affected (0.16 sec)  
  
mysql> flush privileges;  
Query OK, 0 rows affected (0.04 sec)  
  
--客户端远程连接时报:Plugin caching_sha2_password could not be loaded,客户端不支持该加密方式  
mysql> select user, host, plugin, authentication_string from user;  
+------------------+-----------+-----------------------+------------------------------------------------------------------------+  
| user             | host      | plugin                | authentication_string                                                  |  
+------------------+-----------+-----------------------+------------------------------------------------------------------------+  
| root             | %         | caching_sha2_password | $A$005$%z/Joxf//e8}+nUO01.YASUzG9p3CRWbrp5nz/u8O4gy/aEEnV8xqPIDD |  
| mysql.infoschema | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |  
| mysql.session    | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |  
| mysql.sys        | localhost | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE                              |  
+------------------+-----------+-----------------------+------------------------------------------------------------------------+  
4 rows in set (0.00 sec)  
  
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'root';  
Query OK, 0 rows affected (0.10 sec)  
  
--重新修改密码后可连接成功  
mysql> alter  user 'root'@'%' identified by '123456';  
Query OK, 0 rows affected (0.23 sec)  
  
mysql> flush privileges;  
Query OK, 0 rows affected (0.04 sec)