Docker is an open source application container engine that allows developers to package their applications and dependencies into a portable container, and then publish it to any popular Linux machine, which can also be virtualized. Containers use the sandbox mechanism completely, and there will be no interfaces between them.
The above is the introduction of Docker
in Baidu Encyclopedia. Because of the many benefits of Docker
, it is ready to be used in products. Recently, some technical pre-research has been done. This article mainly introduces the use of Docker
in CentOS7
to install MySql
.
In https://hub.docker.com, search for mysql
, the results are as follows:
Select the image in the red box above and execute the following command to install the image
docker pull mysql/mysql-server
Execute the following command to start the container
docker run -d -p:3307:3306--name mysqltest mysql/mysql-server
After the container is started successfully, you cannot connect to MySql
through tools at this time, you need to enter MySql
to make related settings.
First execute the following command to view the container log and find the password of the root
account of MySql
docker logs mysqltest
Find the red box in the picture below is the password of the root
account
Execute the following command to enter the container, there are many ways to enter the container, refer to https://www.cnblogs.com/xhyan/p/6593075.html
docker exec -it mysqltest bash
Then execute the command to enter MySql
mysql -uroot -p
You will be prompted to enter the password. The password is the password in the red box in the above figure. If you see the welcome interface in the figure below, it means that the password is correct and you have entered the environment of MySql
To modify the password of the root account, there are many SQL statements to modify the password on the Internet as follows
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('password123');
But the above statement will report an error in the MySql8.0.11
version
If your version is the same as mine, please execute the following code to modify the root
password
alter user 'root'@'localhost' identified by 'password123';
After modifying the root password, you can use the following code to switch to the mysql database
use mysql
View user information
select user,host from user
You can see that the host
of root
is localhost
, indicating that the root
account cannot be connected externally. Now let's create a new user and assign relevant permissions to allow external connections to be executed.
CREATE USER 'fengwei'@'localhost' IDENTIFIED BY 'password123';
GRANT ALL PRIVILEGES ON *.* TO 'fengwei'@'localhost' WITH GRANT OPTION;
CREATE USER 'fengwei'@'%' IDENTIFIED BY 'password123';
GRANT ALL PRIVILEGES ON *.* TO 'fengwei'@'%' WITH GRANT OPTION;
Execute the exit
command twice to return to CentOS
, execute the following command to restart the MySql
container, and the MySql
will restart during the container restart
docker restart mysqltest
At this moment, we use Sqlyog
to connect to the container. Under the test, we found that the following error will be reported
Execute the command to enter the MySql
in the container, and execute the following Sql
statement
ALTER USER 'fengwei'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password123';
Use SQLyog
to test and find that the connection is successful.
Although the method described in this article can finally connect successfully, the configuration file and data of MySql
are in the container. If the container cannot be started due to configuration reasons, the data content will be lost, so it is better to save the configuration file and data The storage is attached to the [Host] (https://cloud.tencent.com/product/cdh?from=10680). The next article will introduce how to attach the configuration file and data directory to the host in the container of MySql
.
Recommended Posts