SoFunction
Updated on 2025-03-10

Detailed explanation of Docker creating Mysql container and connecting to the container through the command line

Pull NetEase Honeycomb's mysql-server: 5.6

docker pull ./nce2/mysql:5.6

Create mysql5.6 container 1master+3 slaves

docker run --name mysql-master -d -P ./nce2/mysql:5.6
docker run --name mysql-slave1 -d -P ./nce2/mysql:5.6
docker run --name mysql-slave2 -d -P ./nce2/mysql:5.6
docker run --name mysql-slave3 -d -P ./nce2/mysql:5.6

Verify container status

[root@bogon ~]# docker ps
CONTAINER ID    IMAGE             COMMAND       CREATED       STATUS       PORTS        NAMES
907bbbf25d25    ./nce2/mysql:5.6  "/"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave3
a81df6c86808    ./nce2/mysql:5.6  "/"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave2
375eabd4c598    ./nce2/mysql:5.6  "/"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave1
1651d1cab219    ./nce2/mysql:5.6  "/"      14 minutes ago   Up 14 minutes    3306/tcp      mysql-master

Enter the master container through the host command line

docker exec -it mysql-master bash
[root@bogon ~]# docker exec -it mysql-master bash
root@1651d1cab219:/#

Create a database test_docker in master

root@1651d1cab219:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
+--------------------+
5 rows in set (0.02 sec)

mysql> create database test_docker;
Query OK, 1 row affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
| test_docker    |
+--------------------+
6 rows in set (0.00 sec)

Create a database test_docker in slave1

[root@bogon ~]# docker exec -it mysql-slave bash
Error response from daemon: No such container: mysql-slave
[root@bogon ~]# docker exec -it mysql-slave1 bash
root@375eabd4c598:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
+--------------------+
5 rows in set (0.00 sec)

Through the above information, it is shown that master and slave are data isolated, so we can create N mysql containers through docker, and then we can learn the data model in "High Availability MySQL" at a very small cost. We no longer have to worry about insufficient machines.

Follow-up operations

Log in to the master container

[root@bogon ~]# docker exec -it mysql-master bash
root@1651d1cab219:/#

How to view the operating system environment of the container

Usually it's

uname -a
 cat /etc/pro
 cat /etc/lsb-release

Fortunately, our container is ubuntu14.04

root@1651d1cab219:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"

But when executing apt-get install, nothing can be installed

Need to change

cd /etc/apt/

Without vivim ee editor, I have to add content to

 echo deb http://mirrors./ubuntu/ trusty main restricted universe multiverse >> 
 echo deb http://mirrors./ubuntu/ trusty-security main restricted universe multiverse >> 
 echo deb http://mirrors./ubuntu/ trusty-updates main restricted universe multiverse >> 
 echo deb http://mirrors./ubuntu/ trusty-proposed main restricted universe multiverse >> 
 echo deb http://mirrors./ubuntu/ trusty-backports main restricted universe multiverse >> 
 echo deb-src http://mirrors./ubuntu/ trusty main restricted universe multiverse >> 
 echo deb-src http://mirrors./ubuntu/ trusty-security main restricted universe multiverse >> 
 echo deb-src http://mirrors./ubuntu/ trusty-updates main restricted universe multiverse >> 
 echo deb-src http://mirrors./ubuntu/ trusty-proposed main restricted universe multiverse >> 
 echo deb-src http://mirrors./ubuntu/ trusty-backports main restricted universe multiverse >> 

Then update the source

apt-get update
apt-get install vim

Then delete the first two lines of the file through vim and update it again.
apt-get update

Install a network tool to get IP

apt-get install net-tools

Get the IP address of the master

root@1651d1cab219:/# ifconfig
eth0   Link encap:Ethernet HWaddr 02:42:ac:11:00:02
     inet addr:172.17.0.2 Bcast:0.0.0.0 Mask:255.255.0.0
     inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:15119 errors:0 dropped:0 overruns:0 frame:0
     TX packets:12633 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:34197557 (34.1 MB) TX bytes:897732 (897.7 KB)

lo    Link encap:Local Loopback
     inet addr:127.0.0.1 Mask:255.0.0.0
     inet6 addr: ::1/128 Scope:Host
     UP LOOPBACK RUNNING MTU:65536 Metric:1
     RX packets:22 errors:0 dropped:0 overruns:0 frame:0
     TX packets:22 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:2212 (2.2 KB) TX bytes:2212 (2.2 KB)

The slave needs to do the same, too

There is another way

You can create a Dockerfile that depends on mysql image to create a new image.

The new container created by RUN will be executed. There will be installed software.

Finally, connect to the master container on the mysql server through slave docker

Master's server mysql account root assignment permission

mysql> grant all privileges on *.* to root@'%' identified by '';
Query OK, 0 rows affected (0.02 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

The slave server executes the following command

[root@bogon ~]# mysql -uroot -p -h 172.17.0.2
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.19-v1-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]>

Delete the test_docker database on master to see if the slave terminal does not display the deleted library

Master operation

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
| test_docker    |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database test_docker;
Query OK, 0 rows affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
+--------------------+
5 rows in set (0.00 sec)

slave operation

MySQL [(none)]> show databases;
+--------------------+
| Database      |
+--------------------+
| information_schema |
| #bak_database   |
| mysql       |
| performance_schema |
| test        |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.