[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 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时报错
[iyunv@loovelj support-files]# /etc/init.d/mysqld start
Starting MySQL...The server quit without updating PID file [FAILED]cal/mysql/data/liujun.pid).
经过查询,发现已经有一个运行的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>
成功!
参考: