首页 > 数据库 > MySQL > 正文

详解Docker创建Mysql容器并通过命令行连接到容器

2019-11-02 16:50:31
字体:
来源:转载
供稿:网友

拉取网易蜂巢的mysql-server:5.6

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

创建mysql5.6容器 1master+3个slave

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

验证容器状态

[root@bogon ~]# docker psCONTAINER ID    IMAGE             COMMAND       CREATED       STATUS       PORTS        NAMES907bbbf25d25    hub.c.163.com/nce2/mysql:5.6  "/run.sh"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave3a81df6c86808    hub.c.163.com/nce2/mysql:5.6  "/run.sh"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave2375eabd4c598    hub.c.163.com/nce2/mysql:5.6  "/run.sh"      5 minutes ago    Up 5 minutes    3306/tcp      mysql-slave11651d1cab219    hub.c.163.com/nce2/mysql:5.6  "/run.sh"      14 minutes ago   Up 14 minutes    3306/tcp      mysql-master

通过主机命令行进入master容器

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

在master中创建一个数据库test_docker

root@1651d1cab219:/# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 4Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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)

在slave1中创建一个数据库test_docker

[root@bogon ~]# docker exec -it mysql-slave bashError response from daemon: No such container: mysql-slave[root@bogon ~]# docker exec -it mysql-slave1 bashroot@375eabd4c598:/# mysql -uroot -pEnter password:Welcome to the MySQL monitor. Commands end with ; or /g.Your MySQL connection id is 4Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表