How to connect to MySQL server on another host?

Django can simply connect to its MySQL server by setting HOST and PORT in settings.py as `` (empty line):

 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 a different host? Suppose my device is 192.168.1.33 and the other connected computer is 192.168.1.34 , both are on the same network. I tried to set this as:

 'HOST': '192.168.1.34', 'PORT': '3306', 

and

 'HOST': '192.168.1.34', 'PORT': '', 

but both raised this OperationalError :

 (2003, "Can't connect to MySQL server on '192.168.1.34'(10061)") 

SOLUTION: credit @cdhowie

  • Enter the address of the configuration binding to the host you want in /etc/mysql/my.cnf

  • Create a new user for the host you want to grant access to (if you do not have one).

  • Check privileges for this user (if access is denied).

+7
source share
1 answer

By default, Debian-based distributions configure MySQL to bind only to the local host, which means that other hosts cannot connect to it. Correct the MySQL configuration and it will work.

Modify /etc/mysql/my.cnf and change this line:

 bind-address = 127.0.0.1 

For this:

 bind-address = 0.0.0.0 

This will provide MySQL to all network interfaces, so make sure you have security measures if this server is exposed to untrusted hosts.

+22
source

All Articles