- 啟動一個 mysql-server,詳細參數可以參照說明文檔:https://hub.docker.com/r/mysql/mysql-server/,設定密碼,挂載資料庫 等等
wilker@ubuntu:~$ docker pull mysql/mysql-server # 拉一個官方的鏡像下來
wilker@ubuntu:~$ docker run --name=mysql_server11 -e MYSQL_ROOT_PASSWORD= -d mysql/mysql-server # 啟動容器,并設定 root 使用者密碼
- 進入 mysql_server11 容器中修改一下 root 使用者可以通路的ip位址,預設是 localhost,别的ip是連不進來的,這裡改成 任意 ip 都可以連進來(生産環境不能這樣幹)
[email protected]:~$ docker exec -it mysql_server11 mysql -uroot -p123456 # 進入容器的mysql服務
mysql: [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 4
Server version: 5.7.18 MySQL Community Server (GPL)
mysql> show databases; # 看下預設的資料庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> use mysql; # 使用 mysql 資料庫
Database changed
mysql> select host,user from user where user='root';
+-----------+------+
| host | user |
+-----------+------+
| localhost | root | # 預設隻有 目前容器能用 root 使用者連進來,要修改一下
+-----------+------+
1 row in set (0.00 sec)
mysql> update user set host = '%' where user ='root'; # 修改成 % 表示任意ip
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select host,user from user where user='root'; # 檢視一下修改成功沒
+------+------+
| host | user |
+------+------+
| % | root |
+------+------+
1 row in set (0.00 sec)
mysql> flush privileges; # 重新整理權限使之生效
Query OK, 0 rows affected (0.01 sec)
mysql> exit; # 退出 mysql,然後自動退出容器
Bye
[email protected]:~$
- 去 docker hub 拉一個用戶端下來,使用 link 連接配接 mysql_server11 容器,并使用别名 為 db
[email protected]:~$ docker pull imega/mysql-client # 拉 mysql 用戶端鏡像
[email protected]:~$ docker run --name=mysql_client11 --link=mysql_server11:db -i -t imega/mysql-client mysql -h db -uroot -p123456 # -h 的值為 db,就是 mysql_server11 的ip别名
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: MySQL Community Server (GPL)
Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases; # 成功進入 mysql_server11 容器中的資料庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
rows in set ( sec)
MySQL [(none)]> exit;
Bye
- 退出用戶端容器是容器也就變為 Exit 狀态了,再次進入可以用 start 這個容器
[email protected]:~$ docker start mysql_client11 -a
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: MySQL Community Server (GPL)
Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]>