Install Mysql image in Docker container in Linux (CentOS) environment
1. Install Mysql in the Docker container
docker pull mysql #如果使用docker pull mysql命令,就会下载最新的mysql镜像
docker pull mysql:5.7 #下载5.7版本的mysql
The mysql image is successfully pulled:
[root@Silence /]# sudo docker pull mysql:5.7
5.7: Pulling from library/mysql
bf5952930446: Pull complete
8254623a9871: Pull complete
938e3e06dac4: Pull complete
ea28ebf28884: Pull complete
f3cef38785c2: Pull complete
894f9792565a: Pull complete
1d8a57523420: Pull complete
5f09bf1d31c1: Pull complete
1b6ff254abe7: Pull complete
74310a0bf42d: Pull complete
d398726627fd: Pull complete
Digest: sha256:da58f943b94721d46e87d5de208dc07302a8b13e638cd1d24285d222376d6d84
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
查看mysql镜像是否安装成功
#查看已安装的镜像
[root@Silence /]# sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.7 718a6da099d8 11 days ago 448MB
[root@Silence /]#
2. Create an instance and start Mysql
docker run -p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7
Parameter explanation:
docker run 3306:3306 --name mysql\ #将容器3306端口映射到主机的3306端口
-v /mydata/mysql/log:/var/log/mysql\ #将配置文件挂载到主机
-v /mydata/mysql/data:/var/lib/mysql\ #将日志文件挂载到主机
-v /mydata/mysql/conf:/etc/mysql\ #将配置文件挂载到主机
-e MYSQL_ROOT_PASSWORD=root\ #初始化root用户的密码为root
-d mysql:5.7 #开机启动mysql
The instance is successfully created:
[root@Silence /]# docker run -p 3306:3306 --name mysql \
> -v /mydata/mysql/log:/var/log/mysql \
> -v /mydata/mysql/data:/var/lib/mysql \
> -v /mydata/mysql/conf:/etc/mysql \
> -e MYSQL_ROOT_PASSWORD=root \
> -d mysql:5.7
743df4fba2e9e58c7fb28d6a59ae3c64c7d98be1ca5f474f8a95b06e534554b7
View the running instance in Docker:
[root@Silence /]# docker ps #查看Docker中正在运行的容器
#结果
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743df4fba2e9 mysql:5.7 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
3. Start mysql:
docker start mysql
After this step is executed, we can try to connect to mysql remotely with client tools such as Navicat.
If you are using an Alibaba Cloud server, the remote connection cannot be made because you have not opened the 3306 port of the server.
problem solved
Because I am using Alibaba Cloud server, the remote connection fails after Mysql is installed, so we need to enter the Alibaba Cloud console to open the security group of port 3306.
Enter Alibaba Cloud Management Control Backstage----Cloud Server ECS----Instance
Then click the network and security group of the instance, click Quick Add after entering,
and then check mysql to confirm
. After the configuration is complete, connect through Navicat again and find that it can be connected normally.
4. Mysql configuration: configure character encoding format
Create a new my.cnf file
vi mydata/mysql/conf/my.cnf #mysql的文件路径已经映射到Linux中,所以我们在Linux中新增文件也会出现在mysql容器中
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
Add the above code in the my.cnf file to deal with the problem of garbled characters.
Restart mysql after saving
[root@Silence /]# docker restart mysql #重启mysql
mysql
[root@Silence /]#
Check whether the newly created my.cnf file is added inside the mysql container.
[root@Silence /]# docker exec -it mysql /bin/bash
root@743df4fba2e9:/# #成功进入mysql内部
root@743df4fba2e9:/# ls
bin boot dev docker-entrypoint-initdb.d entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@743df4fba2e9:/# cd etc/mysql/
root@743df4fba2e9:/etc/mysql# ls
my.cnf #发现我们刚刚新增的my.cnf文件已经存在。
root@743df4fba2e9:/etc/mysql#
So far our mysql can be used normally.