[root@iZwz92a65mqaacsvogn2uvZ ~]# docker pull mysql:5.7.25 5.7.25: Pulling from library/mysql 27833a3ba0a5: Pull complete 864c283b3c4b: Pull complete cea281b2278b: Pull complete 8f856c14f5af: Pull complete 9c4f38c23b6f: Pull complete 1b810e1751b3: Pull complete 5479aaef3d30: Pull complete 1d924ec3d520: Pull complete 1ab7ae63ac60: Pull complete 08aa5f3680e9: Pull complete a832d0a0972a: Pull complete Digest: sha256:dba5fed182e64064b688ccd22b2f9cad4ee88608c82f8cff21e17bab8da72b81 Status: Downloaded newer image for mysql:5.7.25 docker.io/library/mysql:5.7.25查看镜像
[root@iZwz92a65mqaacsvogn2uvZ ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest f8f4ffc8092c 2 weeks ago 133MB binaryify/netease_cloud_music_api latest ddb0ab1b462f 2 months ago 164MB mysql 5.7.25 98455b9624a9 2 years ago 372MB [root@iZwz92a65mqaacsvogn2uvZ ~]#运行容器
[root@iZwz92a65mqaacsvogn2uvZ ~]# docker run -d --name mysql -v /data/mysql/data:/var/lib/mysql -v /data/mysql/conf:/etc/mysql/ -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 mysql:5.7.25 bbe3c185365938d763e215eb0e8eb85297444033d855e3e5bdfec7d8e7dc792b [root@iZwz92a65mqaacsvogn2uvZ ~]#进入容器,并且登录mysql 测试
[root@iZwz92a65mqaacsvogn2uvZ ~]# docker exec -it mysql /bin/bash root@bbe3c1853659:/# 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.25 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>查询是否开启了远程连接功能
[root@iZwz92a65mqaacsvogn2uvZ data]# docker exec -it mysql /bin/bash root@08a136923dcc:/# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 5 Server version: 5.7.25 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> user mysql; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'user mysql' at line 1 mysql> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> shelect host,user from user; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'shelect host,user from user' at line 1 mysql> select host,user from user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | localhost | mysql.session | | localhost | mysql.sys | | localhost | root | +-----------+---------------+ 4 rows in set (0.00 sec) mysql>
远程连接mysql> select host,user from user;
±----------±--------------+
| host | user |
±----------±--------------+
| % | root | // 这里为 % 既是开启了远程连接功能
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
±----------±--------------+
4 rows in set (0.00 sec)
测试通过,到这里 docker 安装 mysql 就结束了



