1、下载mysql镜像
# docker pull mysql
2、启动mysql容器
# docker run -itd -v /data:/var/lib/mysql -p 33060:3306 --name mysqldb mysql bash
WARNING: IPv4 forwarding is disabled. Networking will not work.
25d047a99917c6420412f36e1d0e2e6af38fc86539b2184d1ea7e5a47b955011
3、这里我们发现启动mysql容器出现warning,排除警告方式如下:
# vi /etc/sysctl.conf
添加:net.ipv4.ip_forward = 1
生效配置:
# sysctl -p
vm.max_map_count = 262144
net.ipv4.ip_forward = 1
重启网路服务:
# service network restart
Restarting network (via systemctl): [ OK ]
再次启动mysql容器,发现警告消失:
# docker run -itd -v /data:/var/lib/mysql -p 33060:3306 --name mysqldb mysql bash
bbde59a2921981218441851d7527d22be3a0e37d3164fd1e07c4653d5dccc94d
注意:从上面启动mysql容器命令中使用了-v、-p参数,具体意义解释如下:
-v:为启动的容器挂在volume,mysql数据库默认数据目录是容器中的/var/lib/mysql目录,该目录是容器自带的一个volume,如果不为mysql容器单独配置数据目录volume,那么我们会发现重启mysql容器后mysql数据库中数据都丢失了。为了持久化mysql数据,我们在启动mysql容器时添加了-v /data:/var/lib/mysql参数,把主机的/data目录挂载到容器的/var/lib/mysql下。
-p:指定mysql容器到本机的端口映射,将容器3306端口映射到主机的33060端口,这样设置后,我们就可以在局域网访问mysql数据库了。
4、进入mysql容器,启动mysql服务:
# docker exec -it mysqldb bash
root@ff3a4e6288c1:/# service mysql status
[info] MySQL Community Server 5.7.21 is not running.
root@ff3a4e6288c1:/# service mysql start
No directory, logging in with HOME=/
..
[info] MySQL Community Server 5.7.21 is started.
5、登录mysql数据库,进行基本配置:
root@ff3a4e6288c1:/# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chavin |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all privileges on *.* to root@'%' identified by 'mysql';
Query OK, 0 rows affected, 1 warning (0.00 sec)
此时,可以在局域网内访问docker容器启动的mysql数据库了。
# mysql -uroot -pmysql -h192.168.177.129 -P33060