先去下载镜像
[root@localhost admin]# docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
9fc222b64b0a: Pull complete
291e388076f0: Pull complete
d6634415290b: Pull complete
1f1e7d852ad4: Pull complete
125fc05f36e0: Pull complete
2aed16e5b02f: Pull complete
5fa9342b7235: Pull complete
6ce062d9949d: Pull complete
c5a4e96aaa50: Pull complete
60ca60d28457: Pull complete
e93ef9cc9bb0: Pull complete
9bc6d9fa3f72: Pull complete
Digest: sha256:01cf53f2538aa805bda591d83f107c394adca8d31f98eacd3654e282dada3193
Status: Downloaded newer imageformysql:latest
docker.io/library/mysql:latest
[root@localhost admin]#
然后查看
[root@localhost admin]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
zookeeper latest e7c648f28c7818hours ago 225MB192.168.192.128:443/hello latest e00eef1cbfb5 3days ago 660MB
mysql latest 62a9f311b99c2weeks ago 445MB
registry latest f32a97de94e15 months ago 25.8MB
java latest d23bdf5b1b1b2years ago 643MB
[root@localhost admin]#
可以看到,MySQL的镜像。启动并查看
[root@localhost admin]# docker run -d -p 3307:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=1234mysql
fe8b5d044db251b86b33981ffb4dac972e98925327e853b7e46d5666dca0c628
[root@localhost admin]# dockerpsCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe8b5d044db2 mysql"docker-entrypoint.s…" 5 seconds ago Up 3 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp some-mysql
[root@localhost admin]#
配置防火墙
[root@localhost admin]# firewall-cmd --zone=public --add-port=3307/tcp --permanent
success
[root@localhost admin]# systemctl restart firewalld
登录MySQL,新增一个用户
[root@localhost admin]# docker exec -it fe8b5d044db2 mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connectionid is 8Server version:8.0.17 MySQL Community Server -GPL
Copyright (c)2000, 2019, 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 clearthe current input statement.
mysql> CREATE USER 'docker-mysql-user'@'%' IDENTIFIED BY '1234';
Query OK,0 rows affected (0.33sec)
mysql> GRANT ALL ON *.* TO 'docker-mysql-user'@'%';
Query OK,0 rows affected (0.00 sec)
设置好之后,我们用本地的Navicat连一下
Docker里新建一个表
看一下客户端有没有