1:獲取MySQL鏡像
運行 docker pull mysql
[root@MyCentos7-1 ~]# docker pull mysql Using default tag: latest latest: Pulling from library/mysql 85b1f47fba49: Pull complete 5671503d4f93: Pull complete 3b43b3b913cb: Pull complete 4fbb803665d0: Pull complete 05808866e6f9: Pull complete 1d8c65d48cfa: Pull complete e189e187b2b5: Pull complete 02d3e6011ee8: Pull complete d43b32d5ce04: Pull complete 2a809168ab45: Pull complete Digest: sha256:1a2f9361228e9b10b4c77a651b460828514845dc7ac51735b919c2c4aec864b7 Status: Downloaded newer image for mysql:latest
2:啟動MySQL鏡像
[root@MyCentos7-1 ~]# docker run --name=mysql -itd -p 3306:3306 -e MYSQL_ROOT_PASSWORD=abcd123 mysql eb3dbfb0958f5c856323e4d8da60d43194884ff05d7adac1ec059adb66ac7f7b
docker run是啟動容器的命令;
--name:指定了容器的名稱,方便之后進入容器的命令行
-itd:其中,i是交互式操作,t是一個終端,d指的是在后台運行
-p:指在本地生成一個隨機端口,用來映射mysql的3306
端口
-e:設置環境變量
MYSQL_ROOT_PASSWORD=emc123123:指定了mysql的root密碼
mysql:指運行mysql鏡像
3:進入MySQL容器
運行 docker exec -it mysql /bin/bash
[root@MyCentos7-1 ~]# docker exec -it mysql /bin/bash root@my-mysql-v1-nths4:/usr/local/mysql#
4:進入MySQL
運行 mysql -uroot -p
root@my-mysql-v1-nths4:/usr/local/mysql# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.4-m14 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 |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
5:進行配置,使外部工具可以連接
為了安全,首先需要設置root帳號的密碼
mysql> update user set authentication_string = password('root') where user = 'root';
將root
的密碼改為root
。
接着,由於mysql中root執行綁定在了localhost
,因此需要對root進行授權,代碼如下,
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
最后,使用navitecat測試mysql連接,如下,