Ubuntu虚拟机间MySQL远程连接失败问题排查求助
Alright, let's break down the remaining configurations you need to check—you’ve already nailed the basics (user host grant, UFW rules, ping connectivity), so let’s dive into the trickier spots:
Check MySQL’s
bind-addresssetting
By default, MySQL is often configured to only listen on127.0.0.1(local connections only). To allow remote access, you need to update this to bind to all interfaces or the specific IP of your target VM:- Open the MySQL config file (path may vary by version, common locations are
/etc/mysql/mysql.conf.d/mysqld.cnfor/etc/mysql/my.cnf) withsudo nano /etc/mysql/mysql.conf.d/mysqld.cnf - Find the line starting with
bind-addressand change it tobind-address = 0.0.0.0(to allow all IPs) or the static IP of your second VM (192.168.1.80) - Restart MySQL to apply changes:
sudo systemctl restart mysql - Verify it’s listening correctly with
sudo netstat -tulpn | grep mysqld—you should see0.0.0.0:3306or your target IP in the output.
- Open the MySQL config file (path may vary by version, common locations are
Confirm user privileges were properly applied
Even if you updated therootuser’s host to%, you might have forgotten to flush privileges, or the grant statement was incomplete:- Log into MySQL locally on the target VM:
mysql -u root -p - Run
FLUSH PRIVILEGES;to ensure your grant changes take effect - Verify the user’s host with
SELECT user, host FROM mysql.user;—you should see a row forrootwithhostset to% - Double-check your grant statement was correct (e.g.,
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_secure_password';—omitting the database/syntax can cause connection failures)
- Log into MySQL locally on the target VM:
Check if
skip-networkingis enabled
Some MySQL configurations include theskip-networkingparameter, which completely disables TCP/IP connections. Open your MySQL config file and look for this line—if it exists, comment it out with a#and restart the MySQL service.Validate port connectivity beyond ping
Ping only confirms ICMP traffic works, not TCP port 3306. Test if the port is actually reachable from your first VM:- Use
nc -zv 192.168.1.80 3306(install netcat first if needed withsudo apt install netcat) - If it returns
Connection to 192.168.1.80 3306 port [tcp/mysql] succeeded!, the port is open; if not, double-check UFW rules and MySQL’s listening config.
- Use
Inspect MySQL’s error logs for clues
The error log will tell you exactly why connections are failing (e.g., permission denied, binding issues). Access it withsudo tail -f /var/log/mysql/error.log, then attempt your remote connection again. Look for lines like:Host '192.168.x.x' is not allowed to connect to this MySQL server
Can't connect to MySQL server on '192.168.1.80'Verify your remote connection command
It’s easy to miss the-hflag (which specifies the remote host). Make sure you’re using:mysql -h 192.168.1.80 -u root -pIf you changed the default MySQL port (3306), add
--port=your_port_numberto the command.
Start with checking the bind-address and error logs—those are the most common culprits here. Work through each step one by one, and you should track down the issue.
内容的提问来源于stack exchange,提问作者Log

