概述
连接Mysql三种方式:
MySQLdb.connect(host='localhost',port=3306,user='root',passwd='root98',db='mysite') //1
MySQLdb.connect(host='127.0.0.1',port=3306,user='root',passwd='root98',db='mysite') //2
MySQLdb.connect(host='172.16.18.214',port=3306,user='root',passwd='root98',db='mysite') //3 172.16.18.214为本机IP地址
方式2连接正常,可以使用,可能127.0.0.1不使用socket进行连接,待考证;
方式1报错,错误提示如下所示:
Traceback (most recent call last):
File "intoMysql.py", line 305, in <module>
output('localhost', 3306, 'root','iiegc2!@#', 'lxf', "utf8",sql)
File "intoMysql.py", line 17, in output
conn = getDB(host, port, user, passwd, db, charset)
File "intoMysql.py", line 11, in getDB
conn = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd,db=db)
File "/lxf/IP/intoMysql/src/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/lxf/IP/intoMysql/src/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
检查发现/etc/my.cnf中配置的socket不是’/tmp/mysql.sock’,解决方法:
- 连接时指定socket,如下所示。
MySQLdb.connect(host=host, port=port, user=user, passwd=passwd,db=db, unix_socket='/var/lib/mysql/mysql.sock')
修改/etc/my.cnf中的socket。
方式3连接报错,如下所示。
Traceback (most recent call last):
File "intoMysql.py", line 304, in <module>
output('172.16.18.214', 3306, 'root','iiegc2!@#', 'lxf', "utf8",sql)
File "intoMysql.py", line 17, in output
conn = getDB(host, port, user, passwd, db, charset)
File "intoMysql.py", line 11, in getDB
conn = MySQLdb.connect(host=host, port=port, user=user, passwd=passwd,db=db, unix_socket='/var/lib/mysql/mysql.sock')
File "/lxf/IP/intoMysql/src/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/lxf/IP/intoMysql/src/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1045, "Access denied for user 'root'@'master' (using password: YES)")
解决方法待探讨。
未完待续!
最后
以上就是英俊鱼为你收集整理的Python连接Mysql错误解析的全部内容,希望文章能够帮你解决Python连接Mysql错误解析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复