Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

and

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

but both caused this OperationalError:

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

SOLUTION: credit @cdhowie

  1. Config bind-address to the host you want in /etc/mysql/my.cnf

  2. Create a new user for the host you want to give an access (if you don't have one).

  3. Check privileges for that user (if access denied).

like image 338
Protocole Avatar asked Aug 20 '11 06:08

Protocole


People also ask

How do I connect to a database on a different server?

Now go the control panel of the Server B where your Database is. In the control panel's Homepage go the databases section and click the Remote MYSQL option. Then add the Ip address of the Server A and click on add host. Now you can access to the database in Server B while your scripts are running in Server A.

Can't connect to MySQL server on remote host?

To allow remote access to MySQL, you have to comment out bind-address (you did) and skip-networking in the configuration file. Next, you have to make sure the user is allowed remote access. Check your user with this: SELECT User, Host FROM mysql.

How do I connect to a MySQL database using IP address?

Select Connections from the SQL navigation menu. In the Authorized networks section, click Add network and enter the IP address of the machine where the client is installed. Note: The IP address of the instance and the mysql client IP address you authorize must be the same IP version: either IPv4 or IPv6. Click Done.

Can MySQL control the access from different hosts?

MySQL handles this by enabling you to distinguish users on different hosts that happen to have the same name: You can grant one set of privileges for connections by joe from office.example.com , and a different set of privileges for connections by joe from home.example.com .


1 Answers

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.

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

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.

like image 190
cdhowie Avatar answered Sep 23 '22 23:09

cdhowie