Install MySql with Docker in CentOS7

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 .

surroundings

Download mirror

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

Start the MySql container

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.

Set up MySql

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.

to sum up

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

Install MySql with Docker in CentOS7
Install MySQL5.7 in centos7
CentOS7.2 install Mysql5.7.13
CentOS7 install MySQL
CentOS 7 install Docker
CentOS7 install Docker
CentOS install mysql
Centos7 install Docker
CentOS7 install mysql
CentOS 7 install MySQL 5.6
Centos7 install docker18
Install Centos7 operating system in Docker
centos7 install docker
CentOS8 install MySQL8.0
CentOS7 install mysql8
CentOS7 install MySQL8
CentOS 6 install Docker
Centos8 install Docker
centos 7.5 install mysql5.7.17
CentOS container installation in Docker uses MySQL
Install Docker CE in yum under CentOS 7
Install Docker on Centos7
CentOS6.5 offline install MySQL5.6.26
Install php in centos
CentOS 7 install Docker service
Install mysql5.7 under CentOS7
CentOS 7.2 Yum install MySQL 5.6
Centos7 install Mysql8 tutorial
Centos manually install mysql8
Install redis5.0 in CentOS7
Install mysql under Centos 7
Centos7 install Mysql database
CentOS 8-dnf install docker
Install mysql5.1 under CentOS6.5
install Docker on centos6.5
CentOS 7 install Docker CE
Install mysql online on centos
centos install mysql through yum
Install MySQL 8.0.16 on Linux Centos
Install docker transfer on Centos7
Install MySQL under Linux (CentOS 7)
Install docker on Centos system
Centos6 install mysql 5.7.x series
CentOS8 install MySQL8 (pro test)
CentOS7 yum install and start mysql
CentOS Yum compile and install MySQL 5.6
Minimal install JDK 1.8 tutorial in CentOS 7
CentOS 7 install Nginx, PHP, MySQL packages
How to install PHP7.4 in CentOS
How to install mysql in Ubuntu 14.04
Install Percona Server database (in CentOS 8)
Install and configure Docker in Ubuntu
Install java in yum mode in Centos
Install MySQL on Linux CentOS7 (Windows)
CentOS7 yum install and start mysql
Container study notes CentOS7 install Docker
Centos7 and centos8 install mysql5.6 5.7 8.0 so simple
Centos yum install mysql5.6 or above
Install and use docker under CentOS 6.8
How to install HDP2.6 in Centos7.2
1.5 Install Centos7