ubuntu MySQL拒绝远程连接(10061)

MySQL是使用apt-get安装的

1.停止mysql服务

sudo service mysql stop

2.修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf 

bind-address = 127.0.0.1 这行注释掉改为 bind-address = 0.0.0.0

3.使用root登陆mysql

hupeng@hupeng-vm:~$ mysql -u root -p
mysql> grant all on *.* to root@"%" identified by "远程登陆的密码"
mysql> flush privileges;  # 这句可以不要
mysql> quit      

root@"%" 中的通配符%表示可以通过任意的ip进行连接,包括localhost。但对于同一个用户名(如root),如果同时设置了%和具体的ip(localhost, 192.168.1.x), 具体主机ip(如localhost)要比通配符%的优先级高。如果尝试通过mysql -uroot -p登陆,使用的root@localhost身份非root@%身份登陆。

远程登陆的密码可以和本地登陆的密码不同

设置本地登陆的密码

mysql> grant all on *.* to root@localhost identified by "本地登陆的密码"

初始状态

 修改后的

 

本地登陆和远程登陆的密码可以不同

 其实mysql是通过用户名和ip地址的组合区分用户

[hupeng@hupeng-vm:~]$mysql -u root -p
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
# 192.168.1.107为本地地址 [hupeng@hupeng-vm:~]$mysql -u root -p -h 192.168.1.107 mysql> select current_user(); +----------------+ | current_user() | +----------------+ | root@% | +----------------+ 1 row in set (0.00 sec)

注释: root 和root@%是等效的

以test用户为例:

test@%、'test'@'%'、“test”@"%" 以及`test`@`%`互为等价,即形如user_name@host_named的账户名称,user_name和host_name可分别加上成对的单引号或者双引号、反引号

'test@%'等价与test@%@%

 

注意: 如果只是创建‘root'@'%'用户,但不重新授权,默认只能访问information_schema表格

服务器端:

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

mysql> create user 'root'@'%' identified by 'xxx';
Query OK, 0 rows affected (0.00 sec)

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

客户端远程登陆

[hupeng@localhost ~]$ mysql -uroot -p -h 192.168.1.107
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)
原文地址:https://www.cnblogs.com/hupeng1234/p/7003445.html