简体   繁体   English

使用 Python mysql.connector 远程连接 MySQL

[英]Remotely connect to MySQL with Python mysql.connector

The following code (ran from a different machine than the mysql server, within the same LAN), to locally connect to MySQL database using Python3 and mysql.connector works:以下代码(从与 mysql 服务器不同的机器运行,在同一局域网内),使用 Python3 和 mysql.connector 工作在本地连接到 MySQL 数据库:

import mysql.connector
cnx = mysql.connector.connect(host='192.168.0.24', database='import_test',user='user_builder', password='password***', port=3309)

However, the following code, to remotely connect to the same database, does NOT work :但是,下面的代码,远程连接到同一个数据库,不能正常工作

import mysql.connector
cnx = mysql.connector.connect(host='http://imaginarywebsite.ddns.net', database='import_test',user='user_builder', password='password***', port=3309)

Instead, I receive the following error:相反,我收到以下错误:

File "C:\Users\****\AppData\Roaming\Python\Python34\site-packages\mysql\connector\network.py", line 464, in open_connection
     errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'http://imaginarywebsite.ddns.net:3309' (11004 getaddrinfo failed)

Here is an extract of my my.cnf file:这是我的 my.cnf 文件的摘录:

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[mysqld]
innodb_buffer_pool_size=4G
innodb_log_file_size=1024M
innodb_read_io_threads=64
innodb_write_io_threads=64
innodb_io_capacity=7000
innodb_thread_concurrency=0

port            = 3309
bind-address    = 0.0.0.0

So, here is what currently works:所以,这是目前有效的:

  • Connect locally to the databse using 192.168.0.24:3309 address, so I am sure the problem does not come from 'privileges granting' or any login/password/port error.使用 192.168.0.24:3309 地址在本地连接到数据库,所以我确定问题不是来自“权限授予”或任何登录/密码/端口错误。
  • Connect remotely via phpmyadmin using http://imaginarywebsite.ddns.net/phpmyadmin , so I am sure the problem does not come from my DNS server.使用http://imaginarywebsite.ddns.net/phpmyadmin通过 phpmyadmin 远程连接,所以我确定问题不是来自我的 DNS 服务器。

And here are my 3 questions :这是我的 3 个问题:

  1. Any Idea where the problem can come from?任何想法问题可能来自哪里?
  2. Should using SSH connection be a solution to my problem?使用 SSH 连接应该可以解决我的问题吗?
  3. If SSH is the solution, is it possible to use SSH parameters through mysql.connector since it does not seem to be presented in the official documentation?如果 SSH 是解决方案,是否可以通过 mysql.connector 使用 SSH 参数,因为它似乎没有出现在官方文档中?

Thanks.谢谢。

Try to edit you non working example to this (no http in the host)尝试将您的非工作示例编辑为此(主机中没有 http)

import mysql.connector

cnx = mysql.connector.connect(
    host='imaginarywebsite.ddns.net',
    database='import_test',
    user='user_builder',
    password='password***',
    port=3309
)

If you're running PHPMyAdmin on the same server that your mysql daemon is running on, here's what's happening: you have your webserver configured to accept connections from all interfaces, but mysql configured to only accept local connections.如果您在运行 mysql 守护程序的同一台服务器上运行 PHPMyAdmin,则会发生以下情况:您的网络服务器配置为接受来自所有接口的连接,但 mysql 配置为仅接受本地连接。 Since PHPMyAdmin is colocated wit mysql, it will only be making local connections which is why it works but your code doesn't.由于 PHPMyAdmin 与 mysql 位于同一位置,因此它只会建立本地连接,这就是为什么它可以工作,但您的代码却不能。

So, double check that your mysql daemon is configured to listen interfaces.因此,请仔细检查您的 mysql 守护程序是否配置为侦听接口。 You should have a line something like bind-address=0.0.0.0 in your mysql.cnf file.您的 mysql.cnf 文件中应该有一行类似于bind-address=0.0.0.0的内容。 See this answer for more details.有关更多详细信息,请参阅此答案

Actually the answer was to use the global IP address of my server instead of the domain name, probably an issue of name management.其实答案是使用我服务器的全局IP地址而不是域名,可能是名称管理的问题。

Replacing host=' http://imaginarywebsite.ddns.net ' by 'host=85.23.56.****" made it work.将 host=' http://imaginarywebsite.ddns.net ' 替换为 'host=85.23.56.****' 使其工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM