简体   繁体   中英

How to access a MySQL server hosted on a Virtual Machine using another Virtual Machine?

I have created 2 Ubuntu 20.04 VMs using VirtualBox. I have a Django App on one VM and MySQL DB on the other VM. How do I access the MySQL DB using the first VM? I used the inet from the output of ifconfig for the IP Address

What I tried:

On the VM having MySQL DB:

-> Change the 'bind_adress' field to '0.0.0.0' in '/etc/mysql/mysql.conf.d/mysqld.cnf'

-> Created a new user using CREATE USER 'test'@'IP_Address1' IDENTIFIED BY 'password';

-> Ran "sudo ufw allow from IP_Address1 to any port 3306"

On the VM having the Django App:

-> Tried to connect to the Virtual Machine using mysql -u 'test' -h 'IP_ADDRESS2' -p

The error I'm getting:

"Unknown MySQL host 'address'"

PS: I created a NAT network on VirtualBox using File->Preference->Network and attached both the VMs to the network and set Promiscuous Mode to "Allow All"

How or where are you defining IP_Address1 and IP_ADDRESS2?

Specifically, your DB machine needs to know what the IP address of the client machine is when you use your CREATE USER query. So, IP_Address1 must be "known" to that machine. Maybe you just hid the IP address when you posted the question, but it might be easier to use actual IP addresses in both cases until you get connectivity.

For example, if your DB machine is 172.18.1.1 and your client machine is 172.18.1.2, then you should use those IP addresses in your commands listed above

Created a new user using

"CREATE USER 'test'@'172.18.1.2' IDENTIFIED BY 'password';"

...here you're defining the IP address of the client connection.

Then in the client machine:

"mysql -u 'test' -h '172.18.1.1' -p"

...here, telling the client the IP address of the DB machine

More importantly, you might also have to expose the DB to the local network in its configuration settings. You'll also have to deal with any routing issues if the two machines are not on the same local subnet.

There are several articles describing how to configure MySQL for network access, such as: https://www.digitalocean.com/community/tutorials/how-to-allow-remote-access-to-mysql

What have you tried? Can you connect from the command line on the.5 machine? There's a mysql command line tool you can use to connect, which might give you some more information, or have you looked at the MySQL logs to see what it's unhappy about?

It could be something like a permissions issue for the user - creating a user alone is not enough to allow them access to database resources. Maybe try something like:

GRANT ALL ON Database.* TO test@'172.25.1.5' IDENTIFIED BY 'PASSWORD';

I was assuming you had configured the various permissions on the database and were only seeing connectivity issues, but from your responses, it's not clear.

Basically, you need to isolate whether the problem is a user permission issue, an IP restriction issue, a VM issue, a coding issue with your app or something else.

Please provide more feedback about what you've tried if you're still struggling, but this could be one of a number of issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM