使用Python mysql.connector远程连接到MySQL [英] Remotely connect to MySQL with Python mysql.connector

查看:319
本文介绍了使用Python mysql.connector远程连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码(从与mysql服务器不同的计算机上运行,​​位于同一LAN内)使用Python3和mysql.connector在本地连接到MySQL数据库:

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:

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)

这是我的my.cnf文件的摘录:

Here is an extract of my my.cnf file:

!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:

  • 使用192.168.0.24:3309地址在本地连接到数据库,因此我确定问题不是出自特权授予"或任何登录/密码/端口错误.
  • 使用 http://imaginarywebsite.ddns.net/phpmyadmin 通过phpmyadmin进行远程连接,确保问题不是来自我的DNS服务器.
  • 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.
  • Connect remotely via phpmyadmin using http://imaginarywebsite.ddns.net/phpmyadmin, so I am sure the problem does not come from my DNS server.

这是我的3个问题:

  1. 有什么想法可以解决问题吗?
  2. 使用SSH连接可以解决我的问题吗?
  3. 如果使用SSH作为解决方案,由于官方文档中似乎没有介绍SSH.connector,因此可以通过mysql.connector使用SSH参数吗?

谢谢.

推荐答案

实际上,答案是使用服务器的全局IP地址代替域名,这可能是名称管理的问题.

Actually the answer was to use the global IP address of my server instead of the domain name, probably an issue of name management.

用"host = 85.23.56"替换host =' http://imaginarywebsite.ddns.net '. ****"使其生效.

Replacing host='http://imaginarywebsite.ddns.net' by 'host=85.23.56.****" made it work.

这篇关于使用Python mysql.connector远程连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆