最近使用阿裡雲伺服器,學習一下Docker,今天學著使用Docker安裝MySQL。 首先,從阿裡雲的Docker Hub 上pull一個MySQL的image. 查看下載鏡像,就會看到已經有了 名字太長,修改為短的tag 根據鏡像創建容器 啟動MySQL容器 進入MySQL終端 參考: http: ...
最近使用阿裡雲伺服器,學習一下Docker,今天學著使用Docker安裝MySQL。
首先,從阿裡雲的Docker Hub 上pull一個MySQL的image.
[centos@loovelj~]$ docker pull registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7 5.7: Pulling from acs-sample/mysql d4bce7fd68df: Pull complete a3ed95caeb02: Pull complete 01588229585e: Pull complete ada32b818a1a: Pull complete ac7528e308ac: Pull complete 44e3fb8779c7: Pull complete bfcca86efc6a: Pull complete 32da415dff2e: Pull complete aae6d9712a36: Pull complete 3148136ce9cc: Pull complete Digest: sha256:32ff2f404c3bd199aaec2e6d19d91d59673e40d7394732124f91dd72a2e1ed97 Status: Downloaded newer image for registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7
查看下載鏡像,就會看到已經有了
[centos@loovelj~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/ubuntu v1.0 523e7db0e264 11 minutes ago 98.3MB ubuntu latest dd6f76d9cc90 7 days ago 122MB hello-world latest 725dcfab7d63 8 days ago 1.84kB registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql 5.7 ec7e75e5260c 23 months ago 360MB
名字太長,修改為短的tag
[centos@loovelj~]$ docker tag registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql:5.7 mysql:5.7 [centos@loovelj~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE test/ubuntu v1.0 523e7db0e264 12 minutes ago 98.3MB ubuntu latest dd6f76d9cc90 7 days ago 122MB hello-world latest 725dcfab7d63 8 days ago 1.84kB mysql 5.7 ec7e75e5260c 23 months ago 360MB registry.cn-hangzhou.aliyuncs.com/acs-sample/mysql 5.7 ec7e75e5260c 23 months ago 360MB
根據鏡像創建容器
[centos@loovelj~]$ docker create -it mysql:5.7 62c975b37ad25b03914eb61e05088019f37ff9cb049a682ac02f20fac1761a4d
啟動MySQL容器
[centos@loovelj~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306 mysql:5.7 2a7a85124400be6fd47e0d97cf5d602456b1db1a11c6331747fe662481eea537 [centos@loovelj~]$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 2a7a85124400 mysql:5.7 "/entrypoint.sh my..." 9 seconds ago Up 8 seconds 0.0.0.0:3306->3306/tcp mysqlserver 188099665d1e ubuntu:latest "/bin/bash" 23 hours ago Up 23 hours angry_spence
進入MySQL終端
[centos@liujun ~]$ docker exec -it 2a7a85124400 /bin/bash root@2a7a85124400:/# mysql -h 127.0.0.1 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.9 MySQL Community Server (GPL) Copyright (c) 2000, 2015, 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>
3.訪問Mysql資料庫
由於我們在上面使用了-p參數映射了容器的3306埠到宿主機的3306埠,此時我們可以直接訪問宿主機的3306埠來訪問Docker中的mysql服務
mysql -h 127.0.0.1 -u root -p
其中,mysql 報錯,我就在本機重新安裝mysql,參照阿裡雲教程。
但是啟動mysql時報錯
[root@loovelj support-files]# /etc/init.d/mysqld start Starting MySQL...The server quit without updating PID file [FAILED]cal/mysql/data/liujun.pid).
經過查詢,發現已經有一個運行的mysql。關閉後再重啟
[root@liujun support-files]# ps -ef|grep mysqld root 10803 9758 0 13:26 pts/0 00:00:00 grep --color=auto mysqld [root@liujun support-files]# kill -9 10803 bash: kill: (10803) - No such process [root@liujun support-files]# kill -9 9758 Killed [root@liujun mysql]# /etc/init.d/mysqld start Starting MySQL.
再執行mysql時,發現還是報錯,查詢原因是啟動項不在/usr/bin下麵。
ln -s /usr/local/mysql/bin/mysql /usr/bin 做個鏈接即可
後來發現環境變數修改了,但是好像沒有保存好,mysqladmin 命令還是不能用,所以又重新保存環境變數
[centos@liujun ~]$ vim ~/.bash_profile #PATH=$PATH:$HOME/bin:/usr/local/apache/bin
#添加以下列
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib #:wq 保存退出 [centos@liujun ~]$ source ~/.bash_profile
再次運行(第二天再次嘗試的,上次的容器已經退出了)docker run
[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306 mysql:5.7 0cc2009d1d367903a0d4e47a6e69af1bc41e409e194a231b6dd1193cc27bf716 docker: Error response from daemon: driver failed programming external connectivity on endpoint mysqlserver (4e7ecdbc87918be626ac8920214ed76386784c83b572f5cd5d3ff0d46d453bb6): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.
發現已經存在這個容器,只好刪除了重新建立容器
[centos@liujun ~]$ docker rm mysqlserver mysqlserver [centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306 mysql:5.7 9455dbd9c9128e51eee84c29d356fb5dff8d31762179ec2585c563ead08ad413
按照教程,通過mysql 遠程連接就好了,但是說埠已經占有,估計是自己的mysql打開了,關閉本機的mysql
[centos@liujun ~]$ docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=sgcc -d -i -p 3306:3306 mysql:5.7 0cc2009d1d367903a0d4e47a6e69af1bc41e409e194a231b6dd1193cc27bf716 docker: Error response from daemon: driver failed programming external connectivity on endpoint mysqlserver
(4e7ecdbc87918be626ac8920214ed76386784c83b572f5cd5d3ff0d46d453bb6): Error starting userland proxy: listen tcp 0.0.0.0:3306: bind: address already in use.
[centos@liujun ~]$ sudo /etc/init.d/mysql stop [sudo] password for centos: sudo: /etc/init.d/mysql: command not found [centos@liujun ~]$ sudo /etc/init.d/mysqld stop Shutting down MySQL.. [ OK ]
最後再執行連接
[centos@liujun ~]$ mysql -h 127.0.0.1 -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.9 MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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>
成功!
http://blog.sina.com.cn/s/blog_7f2ac7b70102vpyl.html
http://www.cnblogs.com/xiohao/p/5377609.html
https://help.aliyun.com/document_detail/50774.html
關於IP的設置
https://severalnines.com/blog/mysql-docker-containers-understanding-basics