Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

时间:2023-03-09 18:11:33
Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

通过sqlyog连接ubuntu上的mysql报错

Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

试了试python直接连接也报同样的错

Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

那应该就是ubuntu上mysql服务自己的问题了

查看mysql 版本

mysql -V

root@cloud:/etc/mysql# mysql -V
mysql Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3

解决过程

查了下资料,mysql默认情况下,只允许localhost连接,需要手动开放其他所有IP的连接。

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.100' IDENTIFIED BY '123456' WITH GRANT OPTION;
flush privileges;

grant参数说明:

权限名 all: 所有权限
on 库名.表名 *表示所有
to '授权用户名'@'授权IP地址' 所有IP用%
identified by "该授权用户名对应的密码"

只有在user表中有的并匹配所有值才能够连接。

修改了ip为pc的lanip,未生效。修改了ip未PC上端路由器的wanip,未生效。

查看了下用户配置信息,外部IP连接数据库时,会根据当前user中的映射关系来进行放行和通过

select host,user,plugin,authentication_string from mysql.user;

Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

所以上面再执行grant时,将多个root插入进来了。但是authentication_string为空,并没有插入正确。以为这里有问题,手动插入了plugin和密码

update user set authentication_string='123456' where user='root';

登录之后发现authentication_string是明文的,重新修改了下密码为密文:

update user set authentication_string=password('123456') where user='root'

重新使用sqlyog连接还是不行。。。

最后还需要修改/etc/mysql这个目录下的my.cnf文件,打开大概45行有这么一段话

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 127.0.0.1

仅仅监听了localhost的请求,要将bind-address修改为0.0.0.0表示接受所有IP的请求

bind-address		= 127.0.0.1

终于连上了

Ubuntu上mysql, 通过python连接报错Can't connect to MySQL server on xxx (10061)

还是挺激动的。