Docker creates the Mysql container and connects to the container from the command line

  • 2020-06-03 08:47:41
  • OfStack

mysql-server: 5.6


docker pull hub.c.163.com/nce2/mysql:5.6

Create the mysql5.6 container 1master+3 slave


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

Verify container status


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

Enter the master container from the host command line


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

Create a database in master, test_docker


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)

The above information shows that master and slave are data isolated, so we can create N mysql containers from docker, and then learn the data model from High Availability MySQL at little cost without worrying about running out of machines.

The subsequent operation

Log in to the master container


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

How do I view the container's operating system environment

1 kind is


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 with ES64en-ES65en install you can't install anything

You need to change

cd /etc/apt/

There is no vi vim ee editor to append content to sources.list


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

And then update the source


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
0

The first two lines of the sources.list file are then deleted from vim and reinstated under update1.
apt-get update

Install a network tool to get ip


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
1

Gets the ip address for master


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
2

slave needs to do the same

There's another way

You can create Dockerfile and rely on the mysql image to create a new image.

The appellate order is executed via RUN to create a new container that will have installed software.

Finally, it connects to master's container mysql server via slave's docker

master's server mysql account root assigns permissions


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


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
4

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

master operation


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
5

slave operation


docker run --name mysql-master -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave1 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave2 -d -P hub.c.163.com/nce2/mysql:5.6
docker run --name mysql-slave3 -d -P hub.c.163.com/nce2/mysql:5.6
6

Related articles: