下载mysql镜像
docker pull hub.c.163.com/nce2/mysql:5.6
创建mysql5.6容器
docker run --name mymysql -d -P hub.c.163.com/nce2/mysql:5.6
验证容器状态
通过主机进入mymysql容器
docker exec -it mymysql bash
//输出
root@4344add2cca7:/#
登录mysql数据库,查看mysql是否可以正常使用
$ docker exec -it mymysql bash
root@4344add2cca7:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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)
mysql> create database pems;
Query OK, 1 row affected (0.00 sec)
mysql> create user 'pems'@'*' identified by 'pemsroot';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on pems.* to 'pems'@'%' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| #bak_database |
| mysql |
| pems |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
对mysql进行远程授权
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set host='%' where user = 'pems';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
查询容器的ip和容器绑的端口 执行结果如下图
docker-machine env
docker ps
宿主机访问mysql连接地址和端口为:192.168.99.100:32768
docker容器之间的访问 --link=mymysql:db 表示把刚才的mymysql容器重新命名为db,然后你就可以在上面的mysqlClient容器中访问到mymysql容器了。
docker run -it -P --link=mymysql:db --name=mysqlClient hub.c.163.com/nce2/mysql:5.6 /bin/bash
在name为mysqlClient中进行数据库访问
root@708f8e6d9d90:/# mysql -h db -upems -ppemsroot
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
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 |
| pems |
| test |
+--------------------+
3 rows in set (0.00 sec)