Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
996 views
in Technique[技术] by (71.8m points)

mysql - SSH Tunnel for Python MySQLdb connection

I tried creating a SSH tunnel using

ssh -L 3306:localhost:22 <hostip>

Then running my python script to connect via localhost

conn = MySQLdb.connect(host'localhost', port=3306, user='bob', passwd='na', db='test')

However, I receive the following error

(2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

How can I make sure I'm hitting the correct host and not just some problem with the bind?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try changing "localhost" to "127.0.0.1", it should work as you expect. This behavior is detailed in the manual:

UNIX sockets and named pipes don't work over a network, so if you specify a host other than localhost, TCP will be used, and you can specify an odd port if you need to (the default port is 3306):

db=_mysql.connect(host="outhouse", port=3307, passwd="moonpie", db="thangs")

If you really had to, you could connect to the local host with TCP by specifying the full host name, or 127.0.0.1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...