1.pull mysql image
docker pull mysql
2.先最简单启动mysql
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 --name mysql mysql
- 1
** 此处MYSQL_ROOT_PASSWORD是mysql root用户初始密码
不出意外应该可以连接。
3.将数据文件保存在宿主机上,并设置配置文件。
- Win10
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 --name mysql -v d:/data/mysql:/var/lib/mysql -v d:/docker/mysql/my.cnf:/etc/mysql/my.cnf mysql
- 1
- Mac
docker run -d -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 --name mysql -v /data/mysql:/var/lib/mysql -v /data/my.cnf:/etc/mysql/my.cnf mysql
- 1
启动后执行下面命令,确认连接成功。
AMIDdeMacBook-Air:~ amid$ mysql -uroot -p -h 127.0.0.1
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>