[英]How to connect to MySQL server on another host?
Django can simply connect to its own MySQL server by setting HOST
and PORT
in settings.py
as '' (empty string): 通过将
settings.py
HOST
和PORT
settings.py
为''(空字符串),Django可以简单地连接到自己的MySQL服务器:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'dbname', # Or path to database file if using sqlite3.
'USER': 'root', # Not used with sqlite3.
'PASSWORD': 'root', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
My question is how to make it able to connect another database on another host? 我的问题是如何使它能够连接另一台主机上的另一个数据库? Suppose my machine is
192.168.1.33
and another machine to be connected is 192.168.1.34
, both are on the same network. 假设我的机器是
192.168.1.33
,另一台要连接的机器是192.168.1.34
,两者都在同一网络上。 I've tried to set this as: 我试图将其设置为:
'HOST': '192.168.1.34',
'PORT': '3306',
and 和
'HOST': '192.168.1.34',
'PORT': '',
but both caused this OperationalError
: 但两者都导致了这个
OperationalError
:
(2003, "Can't connect to MySQL server on '192.168.1.34'(10061)")
SOLUTION: credit @cdhowie 解决方案:信用@cdhowie
Config bind-address to the host you want in /etc/mysql/my.cnf
在
/etc/mysql/my.cnf
中将绑定地址配置到所需的主机
Create a new user for the host you want to give an access (if you don't have one). 为要授予访问权限的主机创建新用户(如果没有)。
Check privileges for that user (if access denied). 检查该用户的权限(如果访问被拒绝)。
By default, Debian-based distros configure MySQL to bind to localhost only, which means that other hosts cannot connect to it. 默认情况下,基于Debian的发行版将MySQL配置为仅绑定到localhost,这意味着其他主机无法连接到它。 Fix your MySQL configuration and it will work.
修复你的MySQL配置,它会工作。
Edit /etc/mysql/my.cnf
and change this line: 编辑
/etc/mysql/my.cnf
并更改此行:
bind-address = 127.0.0.1
To this: 对此:
bind-address = 0.0.0.0
This will expose MySQL to all network interfaces, so make sure that you have security measures in place if this server is exposed to untrusted hosts. 这将使MySQL暴露给所有网络接口,因此如果此服务器暴露给不受信任的主机,请确保您已采取安全措施。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.