SoFunction
Updated on 2025-03-09

Operation Guide for connecting to the virtual machine MYSQL

To connect the native (host) to the MySQL database on the virtual machine, you need to ensure that the network connection between the virtual machine and the host is normal, and that the MySQL configuration allows external connections. The following are the steps to implement the native connection to the virtual machine MySQL:

Step 1: Confirm the network connection between the virtual machine and the local machine

  1. Confirm that the virtual machine and the host are on the same network

    • If the virtual machine is usedBridge Modeorhost-only network mode (Host-Only Network), then the host and the virtual machine are in the same network and can usually be directly connected.
    • If it is usedNAT mode, additional port forwarding may be required to allow the virtual machine to be accessed by the host.
  2. Get the IP address of the virtual machine: Execute the following command in the virtual machine to obtain the IP address:

ip addr show

or

ifconfig
  • Get a similar192.orSuch an IP address.

Step 2: Configure MySQL to allow external connections

By default, MySQL only allowslocalhost(i.e., local connection) to connect. In order to allow external (i.e. native) connections, some modifications to the MySQL configuration are required.

  • Modify MySQL configuration file

    • Open MySQL configuration file(In most Linux systems, the file path is usually/etc/mysql/or/etc/)。

sudo vi /etc/

Find and modifybind-address: turn upbind-addressConfiguration item, modified to0.0.0.0to allow connections from any IP address:

bind-address = 0.0.0.0

Allow remote users to connect: Ensure that MySQL users can access the database via remote connection. You need to modify the MySQL user permissions to enable the user to connect from other hosts.

Log in to MySQL:

mysql -u root -p

Then grant the user remote connection permission:

GRANT ALL PRIVILEGES ON *.* TO 'your_username'@'%' IDENTIFIED BY 'your_password'; 
FLUSH PRIVILEGES;
  1. The meaning of this command:

    • 'your_username'It is the user name you use to connect to MySQL.
    • '%'Indicates that any IP address is allowed to connect. If you want to limit a specific IP, you can%Replace with the specific IP address, such as'192.'
    • 'your_password'It is the user's password.
  2. If the "ERROR 1819 (HY000): Your password does not satisfy the current policy requirements" error occurs. This is because it does not comply with the password setting specification of mysql. The reason may be that the password setting is too simple and reports an error. If you want to view the complete initial password rules of MySQL, execute the following command after logging in: SHOW VARIABLES LIKE 'validate_password',

    The length of the password is determined by validate_password_length, but it can be modified by the following command

set global validate_password_length=4;

validate_password_policy  The verification policy that determines the password, the default level is MEDIUM (medium), and can be modified to LOW (low) through the following command

set global validate_password_policy=0;
  • After the modification is completed, the password can be set very simply, such as 1234 or something.

  • Restart MySQL service: After modifying the configuration, you need to restart the MySQL service to make the configuration take effect:

sudo systemctl restart mysqld

Step 3: Check the firewall settings

If a firewall is enabled on the virtual machine, make sure that the MySQL port (3306 by default) is open.

Check firewall rules: Check if there are rules to block connections:

sudo firewall-cmd --list-all

Allow 3306 ports: If the firewall blocks port 3306, you can run the following command to allow the port to pass through the firewall:

sudo firewall-cmd --zone=public --add-port=3306/tcp --permanent sudo firewall-cmd --reload

Through the above steps, you should be able to connect from the native machine to the MySQL database on the virtual machine.

This is the article about the operation guide for connecting to the virtual machine MYSQL in this article. For more related content on connecting to the virtual machine MYSQL in this machine, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!