简体   繁体   English

通过 ssh 使用 python mysqldb 连接器无密码连接到远程 mysql 服务器

[英]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 隧道访问。

Authentication to remote mysql server using auth_socket plugin is not possible through sshtunnel, as the plugin requires local access to the socket file.无法通过 sshtunnel 使用 auth_socket 插件对远程 mysql 服务器进行身份验证,因为该插件需要本地访问套接字文件。 See this thread for more information.有关更多信息,请参阅此线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于Python MySQLdb连接的SSH隧道 - SSH Tunnel for Python MySQLdb connection 在Ubuntu 18.04中使用MySQLdb或连接器将Python与MySQL连接 - Connect Python with MySQL using MySQLdb or connector in Ubuntu 18.04 Python使用SSHTunnelForwarder和MySQLdb连接到MySQL服务器 - Python connect to MySQL server using SSHTunnelForwarder and MySQLdb python:使用“ MySQLdb”模块连接到远程mysql服务器 - python: use 'MySQLdb' module to connect to remote mysql server 如何使用python在两台远程计算机之间设置无密码ssh? - How to set up passwordless ssh between two remote machines using python? 带有公共密钥的Python MySQLdb连接的SSH隧道 - SSH Tunnel for Python MySQLdb connection with public key 一种使用 python MySQLdb 连接器禁用 SSL 的方法 - A way to disable SSL using python MySQLdb connector 我需要编写一个 python 脚本,它将建立从一台服务器(server1)到另一台远程服务器(server2)的无密码连接 - I need to write a python script which will make a passwordless connection from one server (server1) to another remote server(server2) 尝试使用python-mysql-connector建立与Mysql服务器的SSL连接时访问被拒绝 - Access denied while trying to establish SSL connection to Mysql server using python-mysql-connector Python 通过 SSH 隧道连接到 MySQL 服务器 - Python connecting to MySQL Server through SSH tunneling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM