尝试使用mysql-connector通过python连接到mysql,但是我收到此错误:
Traceback (most recent call last):
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 509, in open_connection
self.sock.connect(sockaddr)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./get_stf_file_names.py", line 26, in update_aws_script_details
database="service_portal"
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 95, in __init__
self.connect(**kwargs)
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/abstracts.py", line 716, in connect
self._open_connection()
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/connection.py", line 206, in _open_connection
self._socket.open_connection()
File "/var/www/html/cloudlab/env/lib/python3.6/site-packages/mysql/connector/network.py", line 512, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (111 Connection refused)
我检查了mysqld是否正在使用端口3306,它是:
netstat -lnp | grep 3306
这给了:
tcp 0 0 my-ip:3306 0.0.0.0:* LISTEN 12003/mysqld
我也试过这个命令,它完美地工作:
mysql -h localhost -u root -p
(就像在我可以使用此连接到数据库)
我试图通过以下方式访问数据库:
mydb = mysql.connector.connect(host="localhost",user="****",
passwd="****",
database="**********"
)
cursor = mydb.cursor(dictionary=True)
如果我将上面命令中的localhost更改为my-ip,那么它可以正常工作。有什么我做错了吗?
Localhost是一个ip 127.0.0.1。 你的mysql实例是在ip上还是在外部上监听?