如何连接到另一台主机上的MySQL服务器? [英] How to connect to MySQL server on another host?
问题描述
通过在中设置
as''(空字符串): HOST
和 PORT
,Django可以简单地连接到自己的MySQL服务器。 settings.py
Django can simply connect to its own MySQL server by setting HOST
and PORT
in settings.py
as '' (empty string):
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.
}
}
我的问题是如何能够连接另一个数据库在另一台主机上?假设我的机器是 192.168.1.33
,另一台要连接的机器是 192.168.1.34
,它们都在同一个网络。我尝试设置为:
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:
'HOST': '192.168.1.34',
'PORT': '3306',
和
'HOST': '192.168.1.34',
'PORT': '',
但都导致此 OperationalError
:
(2003, "Can't connect to MySQL server on '192.168.1.34'(10061)")
解决方案:信用@cdhowie
SOLUTION: credit @cdhowie
-
配置绑定地址到您想要的主机
etc / mysql / my.cnf
为要授予访问权的主机创建一个新用户
Create a new user for the host you want to give an access (if you don't have one).
检查该用户的权限(如果访问被拒绝)
推荐答案
默认情况下,基于Debian的发行版配置MySQL绑定到localhost,这意味着其他主机无法连接到它。修复您的MySQL配置,并且它将工作。
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.
编辑 /etc/mysql/my.cnf
:
bind-address = 127.0.0.1
到此:
bind-address = 0.0.0.0
这会将MySQL暴露给所有网络接口,因此,如果此服务器暴露给不受信任的
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服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!