先去下载镜像
[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 image for mysql:latest docker.io/library/mysql:latest [root@localhost admin]#
然后查看
[root@localhost admin]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE zookeeper latest e7c648f28c78 18 hours ago 225MB 192.168.192.128:443/hello latest e00eef1cbfb5 3 days ago 660MB mysql latest 62a9f311b99c 2 weeks ago 445MB registry latest f32a97de94e1 5 months ago 25.8MB java latest d23bdf5b1b1b 2 years ago 643MB [root@localhost admin]#
可以看到,MySQL的镜像。启动并查看
[root@localhost admin]# docker run -d -p 3307:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=1234 mysql fe8b5d044db251b86b33981ffb4dac972e98925327e853b7e46d5666dca0c628 [root@localhost admin]# docker ps CONTAINER 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 connection id is 8 Server 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 clear the current input statement. mysql> CREATE USER 'docker-mysql-user'@'%' IDENTIFIED BY '1234'; Query OK, 0 rows affected (0.33 sec) mysql> GRANT ALL ON *.* TO 'docker-mysql-user'@'%'; Query OK, 0 rows affected (0.00 sec)
设置好之后,我们用本地的Navicat连一下
Docker里新建一个表
看一下客户端有没有