[英]Connection to remote mysql server through ssh using python mysqldb connector passwordless
Python 3.8 Python 3.8
Mysql 8.0.23-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu)) Mysql 8.0.23-0ubuntu0.20.04.1 用于 x86_64 上的 Linux((Ubuntu))
Hi,你好,
I want to connect to a distant mysql server using python's mysqldb connector and paramiko's sshtunnelforwarder.我想使用 python 的 mysqldb 连接器和 paramiko 的 sshtunnelforwarder 连接到远程 mysql 服务器。
I can connect to the database remotely without any problems by executing the following:通过执行以下命令,我可以毫无问题地远程连接到数据库:
Connecting to database using mysql password authentication使用mysql密码认证连接数据库
server = new_ssh_server(config)
with server:
print('Connection', server.local_bind_address)
cnx = MySQLdb.connect(host = '127.0.0.1',
port = server.local_bind_port,
user = config['user'],
passwd = config['password'],
db = config['db'])
Queries work, I can read/write to database, no problem.查询工作,我可以读/写数据库,没问题。
I would like to connect to database without supplying mysql password, by using mysql auth_socket authentication method, through ssh.我想通过 ssh 使用 mysql auth_socket 身份验证方法,在不提供 mysql 密码的情况下连接到数据库。
My attempts at this can be resumed by the following code:我可以通过以下代码恢复我的尝试:
Connecting to database using mysql auth_socket authentication使用 mysql auth_socket 认证连接数据库
with server as tunnel:
print('Tunnel:', tunnel.local_bind_address)
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='tutut')#, unix_socket="/tmp/mysql.sock")
res = pd.read_sql('select * from users;', cnx)
print(res)
Which throws the following error:这会引发以下错误:
File "connect_ssh_mysql_auth_socket.py", line 12, in <module>
cnx = MySQLdb.connect(host = 'localhost', user = 'hillbilly', password = '', db='rsotest2')#, unix_socket="/tmp/mysql.sock")
File "...../lib/python3.8/site-packages/MySQLdb/__init__.py", line 84, in Connect
return Connection(*args, **kwargs)
File "...../lib/python3.8/site-packages/MySQLdb/connections.py", line 179, in __init__
super(Connection, self).__init__(*args, **kwargs2)
MySQLdb._exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)")
I have and existing mysql d .sock on the distant server that I symlinked to /tmp/mysql.sock , but the error remains.我在远程服务器上拥有和现有 mysql d .sock ,我符号链接到/tmp/mysql.sock ,但错误仍然存在。 I have also added the next line to /etc/mysql/mysql.conf.d/mysql.cnf :我还在/etc/mysql/mysql.conf.d/mysql.cnf添加了下一行:
socket=/run/mysqld/mysqld.sock
But I still get the same error when trying to connect remotely.但是尝试远程连接时我仍然遇到同样的错误。
Specifying the unix_socket='/run/mysqld/mysqld.sock' to mysqldb connector (commented in Connecting to database using mysql auth_socket authentication ) does not fix the issue.将unix_socket='/run/mysqld/mysqld.sock'指定到 mysqldb 连接器(在Connecting to database using mysql auth_socket authentication中评论)不能解决问题。
I seem to misunderstand the use of mysql.sock, mysqld.sock.好像对mysql.sock、mysqld.sock的使用有误解。 I was not able to find nor create a mysql.sock socket.我无法找到也无法创建 mysql.sock 套接字。 Is what I am trying to do possible?我正在尝试做的事情可能吗? I remember reading somewhere that unix sockets only work locally, does this mean it is not achievable?我记得在某处读到 unix sockets 只能在本地工作,这是否意味着它无法实现?
Any help/explanation would be appreciated.任何帮助/解释将不胜感激。
(EDIT AND CLOSING) So this is not possible. (编辑和关闭)所以这是不可能的。 Following this thread , auth_socket needs local access to the socket file (usually /tmp/mysql.sock) to run autentication tests, so not accessible through ssh tunneling.在这个线程之后,auth_socket 需要本地访问套接字文件(通常是 /tmp/mysql.sock)来运行认证测试,因此不能通过 ssh 隧道访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.