I'm Trying to connect to Mysql server from python. and i'm getting the following error:
Traceback (most recent call last):
File "./db_connection.py", line 2, in <module>
Con = MySQLdb.Connect(host="127.0.0.1",port= 3360, user="username",
passwd="password", db="entries")
File "/usr/lib64/python2.7/site-packages/MySQLdb/__init__.py", line
85, in Connect
return Connection(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py",
line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL
server on '127.0.0.1' (111)")
I checked and my port is 0 :
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 0 |
+---------------+-------+
1 row in set (0.01 sec)
and skip_networking=ON:
mysql> SHOW VARIABLES LIKE 'skip_networking';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| skip_networking | ON |
+-----------------+-------+
1 row in set (0.00 sec)
file my.cnf looks like that:
[mysqld]
skip-grant-tables
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
I tried to put skip-networking = Off and port=3360 and then restart mysql service and it didn't work- skip_networking is still on. Any suggestions on how to connect MySQL DB with python?
Thanks!
If the server is started with the --skip-grant-tables
option to disable authentication checks, the server enables --skip-networking
to prevent remote connections.
Yo should comment the --skip-grant-tables
.
First Thing you need to do is stop your MYSQL server.
if you are using Mac,
sudo /usr/local/mysql/support-files/mysql.server stop
sudo mysqld_safe --skip_networking=0
then you can close the terminal window, and re-open it and connect to the mysql again. your port will be reset and, skip_networking should be reset to OFF.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.