mysql连接不上问题解决

公司新搭内网测试环境,mysql远程登录问题解决
远程登录:
1 修改host,

mysql> select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | auth_socket           |
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+
mysql> alter user 'root'@'%' identified by '123456';
//注意这里如果用update的话 可能会报错:
mysql> update user set authentication_string=password("123456"),plugin='mysql_native_password' where user='root';
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 '("123456"),plugin='mysql_native_password' where user='root'' at line 1

2 配置文件 bind-address = 0.0.0.0 设置这里为你的ip或者全部ip 0.0.0.0代表所有

vim my.cnf

3 防火墙

ufw allow 3306/tcp;
ufw status;
root@base-ubuntu20: ufw allow 3306/tcp
规则已添加
规则已添加 (v6)
root@base-ubuntu20: ufw status;
状态: 激活

至                          动作          来自
-                          --          --
443                        ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
22/tcp                     ALLOW       Anywhere                  
3306/tcp                   ALLOW       Anywhere                  
443 (v6)                   ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             
22/tcp (v6)                ALLOW       Anywhere (v6)             
3306/tcp (v6)              ALLOW       Anywhere (v6)  

4 加密方式(plugin=mysql_native_password),

mysql> update user set plugin='mysql_native_password' where user='root'
    -> ;
Query OK, 1 row affected (0.71 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

5 设置登陆密码(authentication_string)


select host,user,authentication_string,plugin from user;	

update user set plugin='mysql_native_password' where user='root';

alter user 'root'@'%' identified by '123456';
      

以上每一次做完 都flush一次。

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

最后重启mysql 服务 service mysql restart
参考:

https://blog.csdn.net/appleyuchi/article/details/113196484
https://zhuanlan.zhihu.com/p/391311322
https://blog.csdn.net/weixin_51563198/article/details/121529081

或者 如果不想用root。 自己新建一个账号:

create user 'tom'@'%' identified by 'tom123';

grant all privileges on *.* to 'tom'@'%' WITH GRANT OPTION;
flush privileges;

all privileges : 所有权限
*.* : 所有的数据库中的所有的表
WITH GRANT OPTION :权限给分享给自己创建的用户

猜你喜欢

转载自blog.csdn.net/qq_37106501/article/details/129765860