如何连接到另一台主机上的 MySQL 服务器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7130239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to MySQL server on another host?
提问by Protocole
Django can simply connect to its own MySQL server by setting HOST
and PORT
in settings.py
as '' (empty string):
Django 可以通过设置HOST
和PORT
输入settings.py
为 ''(空字符串)来简单地连接到它自己的 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. I've tried to set this as:
我的问题是如何使它能够连接另一台主机上的另一个数据库?假设我的机器是192.168.1.33
,另一台要连接的机器是192.168.1.34
,两者都在同一个网络上。我试图将其设置为:
'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
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).
配置绑定地址到你想要的主机
/etc/mysql/my.cnf
为您要授予访问权限的主机创建一个新用户(如果您没有)。
检查该用户的权限(如果访问被拒绝)。
回答by cdhowie
By default, Debian-based distros configure MySQL to bind to localhost only, which means that other hosts cannot connect to it. Fix your MySQL configuration and it will work.
默认情况下,基于 Debian 的发行版将 MySQL 配置为仅绑定到 localhost,这意味着其他主机无法连接到它。修复您的 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 暴露给所有网络接口,因此如果此服务器暴露给不受信任的主机,请确保您有适当的安全措施。