Docker mysql host access and docker container access

download mysql image

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

 Create mysql5.6 container

docker run --name   mymysql  -d -P  hub.c.163.com/nce2/mysql:5.6

 Verify container status


 Enter the mymysql container through the host

docker  exec   -it   mymysql  bash
// output
root@4344add2cca7:/#

 Log in to the mysql database to see if mysql can be used normally

$ 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)

remote authorization to 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)

 The execution result of querying the ip of the container and the port bound to the container is as follows

docker-machine  env
docker  ps

 

 The host accesses mysql connection address and port: 192.168.99.100:32768

Access between docker containers --link=mymysql:db means rename the mymysql container to db, and then you can access the mymysql container in the mysqlClient container above.

docker  run -it -P --link=mymysql:db   --name=mysqlClient hub.c.163.com/nce2/mysql:5.6 /bin/bash

 Database access in the 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)

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326066780&siteId=291194637