1、拉取镜像
docker pull mysql/mysql-server
2、查看镜像
PS C:\Users\47292> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql/mysql-server 5.7.21 f92f0896ed95 3 years ago 246MB
3、安装镜像
PS C:\Users\47292> docker run --name mysqlserver -v D:\docker\mysql\conf:/etc/mysql/conf.d -v D:\docker\mysql\logs:/logs -v D:\docker\mysql\data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3306:3306 mysql/mysql-server:5.7.21
8b3e23ddd26850196fa156326d4d99d5dcc2ffb646b5fa850b3180ac54d27deb
4、查看镜像
PS C:\Users\47292> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8b3e23ddd268 mysql/mysql-server:5.7.21 "/entrypoint.sh mysq…" Less than a second ago Up 3 seconds (health: starting) 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp mysqlserver
5、进入mysql并创建用户test 和授权给test
PS C:\Users\47292> docker exec -it mysqlserver bash
bash-4.2# ls
bin dev entrypoint.sh healthcheck.cnf home lib64 media mysql-init-complete proc run srv tmp var
boot docker-entrypoint-initdb.d etc healthcheck.sh lib logs mnt opt root sbin sys usr
bash-4.2# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 36
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> 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> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| healthchecker | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql> CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
Query OK, 0 rows affected (0.00 sec)
6、验证是否能登录mysql
参考文章:https://blog.csdn.net/junshangshui/article/details/79432487