Docker installs Mysql and the problems and solutions that may be encountered when installing Mysql

Hello everyone, let's start directly:

First start to pull the mysql image here is the 5.7 version of docker pull mysql:5.7 and then docker images to check whether the image already exists. If it exists, you can start mysql with the following command docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7  At this time, docker ps checks whether mysql has started. If the startup is successful, you can enter the interactive mode through docker exec -it container id bash , and then enter the password just now through mysql -uroot -p to enter the inside of mysql. At this time, this There may be some situations where the Chinese character set cannot be written or the character set encoding is disordered. The most important thing is that the data disappears after we delete the container. At this time, it can be solved as follows:

First of all, we need to perform a mysql volume mapping, map mysql in docker to our virtual machine local, we can create a new directory, I put it in the root directory, so we can execute this command, docker run -d -p 3306:3306 --privileged=true -v /root/mysql/log:/var/log/mysql -v /root/mysql/data:/var/lib/mysql -v /root/mysql/conf:/ etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7  This command is a command to realize the file mapping in our docker container and start it. You will find that this command maps the three most critical The directories are, data , log , conf , these three directories, after running this command, you will find that there will be an extra mysql file under the root path specified locally. After entering cd mysql , you will find that there are three folders. data, log, conf,

 At this time, we need to solve the problem that the character set cannot input Chinese. After entering the mysql folder, enter the following command, cd conf . Check the folder list and find that it is empty. At this time, we need to create a configuration file by ourselves. This file is in mysql. The core file, vim my.cnf , and then add this content:

 

[client]

default_character_set=utf8

[mysqld]

collation_server = utf8_general_ci

character_set_server = utf8

 , this content is to set the mysql character set encoding to be corrected to utf8. Next, we view the running mysql container through docker ps , then through docker exec -it container id bash , then enter the mysql container interactive mode and then pass, mysql -uroot -p , this command enters mysql, first check whether the character set is correct, SHOW VARIABLES LIKE 'character%'; , if it is found to be utf8, it means that there is probably no problem, at this time we can create a new database, create database tb01 ; , use databases; , after building the database, we create a new table, create table t_dog(id int,name varchar(255)); , and then we add a new piece of data in this table, insert into t_dog values(1,' jack'); Note that Chinese cannot be typed when operating the database on the server. After we execute this sentence, we go to our visualization tool to view the data. At this time, we can add a Chinese data verification on the visualization tool, insert into t_dog values (2,"Jack");, at this time, when we check this table, we will find that the Chinese data has been added successfully

Next, we try to delete the mysql container in docker to see if our data will be deleted or lost.

We first use docker ps to view the running container, and then delete the container with docker rm -f container id. At this time, we will find that the container has been deleted by viewing docker ps. Next, we execute this command docker run -d  - p 3306:3306 --privileged=true -v /root/mysql/log:/var/log/mysql -v /root/mysql/data:/var/lib/mysql -v /root/mysql/conf:/etc /mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 --name mysql mysql:5.7 , then restart the container, enter mysql and pass  show databases; check our database, you will find that the database we created is still there, at this time you can pass use tb01; , switch to this database through select * from t_dog;

 , the data still exists, indicating that we have operated into a function, and then we can happily play with our mysql through docker

If this article is helpful to everyone, give it a like, thank you guys, see you in the next article!

 

Guess you like

Origin blog.csdn.net/m0_67864787/article/details/131452247