阿里云服务器安装mysql后本地连接失败

阿里云服务器安装mysql后本地连接失败

一、问题描述

在阿里云安装mysql后,想在本地电脑用可视化工具连接mysql,但是提示连接失败

错误如图所示:

二、问题分析

1、检查3306端口

首先,检查阿里云服务器的安全组是否开放了3306端口

可以看到,我已经放行了3306端口。

2、检查防火墙

我选择直接关闭防火墙,我是centos7,所以执行如下命令:

[root@root /]# systemctl stop firewalld

3、检查是否允许root远程登录

3.1、通过xshell连接云服务器后,通过root登录上mysql

[root@root /]# mysql -h localhost -P 3306 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

然后,输入密码,登录成功

3.2、切换到mysql库

mysql> use mysql;
Database changed

3.3、查看root用户是否允许远程

mysql> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)

可以看到root用户只允许本地登录,但是不允许远程登录。

3.4、修改root用户允许远程登录

mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

3.5、刷新权限

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

3.6、再次查看root用户是否允许远程登录

mysql> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
+---------------+-----------+
 rows in set (0.00 sec)

三、问题解决

查看连接结果

连接成功!!!

posted @ 2024-03-23 13:55  树叶的一生啊  阅读(16)  评论(1编辑  收藏  举报