简体   繁体   中英

Cannot connect to MySQL installed on GCP Virtual Machine

I've installed MySQL on a Google Cloud Virtual Machine (debian buster). What I want to do is to make this accessible publicly (using username / password obviously).

As far as I can tell the server is visible from the outside world, as I can ping the IP and I get results, and I think I've set up a user correctly and given the appropriate permissions so I can log in.

For info, my firewall settings on GCP look like this (this is just the egress, there is one exactly the same for ingress):

在此处输入图像描述

Which I'm assuming is correct and leaves the correct port open?

The issue I have when I use MySQL Workbench is that when I try to create a new connection, it gives me the following error:

Your connection attempt failed for user 'username' to the MySQL server at [my ip address]:3306:

Unable to connect to localhost

Please:

1 Check that MySQL is running on address [my ip address]

2 Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)

3 Check the user username has rights to connect to [my ip address] from your address (MySQL rights define what clients can connect to the server and from which machines)

4 Make sure you are both providing a password if needed and using the correct password for [my ip address] connecting from the host address you're connecting from**

Any pointers would be gratefully received.

Update: What is really confusing me is the 'Unable to connect to localhost' error. I'm not trying to connect to localhost...?

Update 2: As per comments, results of the following commands:

在此处输入图像描述

在此处输入图像描述

Note I am trying to connect using the matprichardson username. The svc2toria user is pointing to my own IP address.

Mat, If you want to use your Google Cloud Instance Database using your MySQL workbench. I suggest you connect to it through an SSH tunnel. So, this problem won't happen. I also ran into this problem several times. Connecting through SSH made the job done.

在此处输入图像描述

But if your need is something else, this would not help you at all. If your only purpose is managing your database from your local machine using the MySQL workbench. This will work nicely. Create a USER in your Debian VM. and open port 22 to the public. Also, make sure to have strong credentials or a better key file when connecting through SSH. This method is working for every cloud VM database. I'm using this method for G-Cloud, Azure, and AWS. After all of your work is done. Close port 22 (SSH).

My best guess will be because of number 2. "Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)".

Your Virtual Machine will have network security controls / firewall which will be blocking port 3306 by default.

I don't use Google cloud but I believe you are looking for "network details" -> "Firewall rules".

did you change your mysqld.cnf already?

bind-address           = 0.0.0.0

As none of the suggestions posted worked I went for the rather more nuclear option of deleting and rebuilding my VM and setting MySQL up again from scratch. I must have done something wrong in my initial setup, as things worked without any issues at all once I'd done this.

The location of the MySQL configuration file differs depending on the distribution. In Ubuntu and Debian the file is located at /etc/mysql/mysql.conf.d/mysqld.cnf

while in Red Hat based distributions such as CentOS, the file is located at /etc/my.cnf

Open the file with your text editor:

sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf

Search for a line that begins with bind-address and set its value to the IP address on which a MySQL server should listen.

By default, the value is set to 127.0.0.1 (listens only in localhost).

In this example, we'll set the MySQL server to listen on all IPv4 interfaces by changing the value to 0.0.0.0

bind-address           = 0.0.0.0

# skip-networking

If there is a line containing skip.networking, delete it or comment it out by adding # at the beginning of the line.

In MySQL 8.0 and higher, the bind-address directive may not be present. In this case, add it under the [mysqld] section.

Once done, restart the MySQL service for changes to take effect. Only root or users with sudo privileges can restart services.

To restart the MySQL service on Debian or Ubuntu, type:

sudo systemctl restart mysql

On RedHat based distributions like CentOS to restart the service run:

sudo systemctl restart mysqld

For more Detail Read Here

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