Selenium遇到的问题7 无法连接MySQL
Python要连接MySQL,并操作首先要下载一个MySQL-Python
下载地址:https://pypi.python.org/pypi/MySQL-python/1.2.5
连接mysql并获取数据库的代码:
import MySQLdb try: conn = MySQLdb.connect(host = 'localhost', user = 'root', passwd = 'xxxxxx', port =3306) cur = conn.cursor() res = cur.execute('show databases') #输出数据库里所有的表 for data in cur.fetchall(): print '%s' % data cur.close() conn.cursor() except MySQLdb.Error, e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])
python连接mysql出现的bug:
Mysql Error 2003: Can't connect to MySQL server on 'localhost' (10061)
后来发现我原来装的MySQL常用的host是127.0.0.1,修改代码后成功连接。
解决方案:
将host = ‘localhost’ 改为 host = ‘127.0.0.1’
结果: