Centos 7 installs MySQL 8.0.33 through Docker to achieve data persistence and my.cnf configuration

1. Docker starts the MySQL container to achieve data persistence

To use Docker to start MySQL 8.0.33 on CentOS 7, configure the my.cnf file of MySQL, and realize the persistence of MySQL data, you can follow the steps below:

1. Install Docker: Make sure you have installed Docker on CentOS 7. If it is not installed, follow the instructions provided by the official Docker documentation to install it.

2. Create a persistent storage directory: In order to achieve data persistence, we will create a directory for storing MySQL data. Let's say we named it /opt/mysql_data. Run the following command in a terminal to create the directory:

sudo mkdir -p /opt/mysql_data


3. Pull the MySQL image: Run the following command to pull the MySQL 8.0.33 image from Docker Hub:

sudo docker pull mysql:8.0.33


4. Start the MySQL container: run the following command to start the MySQL container, and mount the persistent storage directory and the custom my.cnf file at the same time:

You need to manually create a new /path/to/my.cnf file in advance,

----Because I have individual requirements, I need to set the /path/to/my.cnf file to server_id=2 (no such requirement, can be ignored)

Start the docker container

sudo docker run -d --name mysql-container \
  -v /opt/mysql_data:/var/lib/mysql \
  -v /path/to/my.cnf:/etc/mysql/my.cnf \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -p 3305:3306 \
  mysql:8.0.33

  • Replace /opt/mysql_data with the path to the persistent storage directory you created.
  • Replace /path/to/my.cnf with my custom my.cnf file path.
  • Replace your_password with the MySQL root password you want to set.

My password is 123456

/path/to/my.cnf

The port is 3305


5. Verify the configuration: Wait a moment, the MySQL container will start and load the custom my.cnf file. You can verify that the configuration is working by connecting to the MySQL container:

sudo docker exec -it mysql-container mysql -u root -p

 
After entering the previously set password, enter the MySQL client.

The password is 123456

6. Execute the following command in the MySQL client to verify whether the server_id configuration has been set to 2:

SHOW VARIABLES LIKE 'server_id';


If the value of server_id is 2, the configuration in my.cnf has been applied successfully.

The MySQL container is now running persistently on CentOS 7 with a custom my.cnf configuration file with a server_id value of 2.

Note that every time you modify the my.cnf file, you need to restart the MySQL container for the changes to take effect:

sudo docker restart mysql-container

2. Set up the MySQL container to support remote access

By default, Docker's container networking uses bridged mode, so services inside containers can only be accessed through the container's IP address. You need to modify the container's firewall rules to allow connections from remote hosts.

1. Enter the docker-mysql container and enter MySQL

sudo docker exec -it mysql-container mysql -u root -p

Password 123456

2. Reset the password

use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

3. Refresh permissions

flush privileges;

4. Enable remote access and modify the default password and encryption method

alter user 'root'@'%' identified with mysql_native_password by 'root';

Now you can connect remotely, use the ip address of the service, and the port number 3305, account root password 123456

Remember that the server should open port 3305 to the outside world

Guess you like

Origin blog.csdn.net/qq_39208536/article/details/132354310