参考 https://www.runoob.com/docker/docker-install-mysql.html
访问 MySQL 镜像库地址:https://hub.docker.com/_/mysql?tab=tags 。
docker pull mysql
或者 docker pull mysql:latest
docker images
docker inspect mysql
版本为 mysql-8.0
得知 mysql-8 版本有不兼容的问题,换成 mysql-5.7.30
docker rmi mysql
docker pull mysql:5.7.30
docker inspect mysql:5.7.30
版本为 mysql-5.7.30
mysql镜像中的默认配置
日志位置:/var/log/mysql/
配置文件:/etc/mysql/conf.d/, /etc/mysql/mysql.conf.d/
数据位置:/var/lib/mysql/
特别注意点是,mysql的数据目录应该映射到本机,否则重启mysql容器有可能会导致数据丢失!!!
使用host网络启动
docker run --name mysql --network host -p 3749:3749 -v /data/dockers/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7.30
docker psnetstat -lntp
如果端口3749没有生效的话,需要进入容器修改配置文件
vi /etc/mysql/mysql.conf.d/mysqld.cnf
增加 port = 3749
docker exec -it mysql bash
mysql -h localhost -u root -p
查看配置文件
cat /etc/mysql/my.cnf
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License, version 2.0,# as published by the Free Software Foundation.## This program is also distributed with certain software (including# but not limited to OpenSSL) that is licensed under separate terms,# as designated in a particular file or component or in included license# documentation. The authors of MySQL hereby grant you an additional# permission to link the program and your derivative works with the# separately licensed software that they have included with MySQL.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License, version 2.0, for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
ls /etc/mysql/conf.d/
docker.cnf mysql.cnf mysqldump.cnf
ls /etc/mysql/mysql.conf.d/
mysqld.cnf
cat /etc/mysql/conf.d/docker.cnf
[mysqld]
skip-host-cache
skip-name-resolve
cat /etc/mysql/conf.d/mysql.cnf
[mysql]cat /etc/mysql/conf.d/mysqldump.cnf
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
修改监听地址
vi /etc/mysql/mysql.conf.d/mysqld.cnf
bind-address = 0.0.0.0
exit
docker restart mysql
crontab添加定时任务备份,每天凌晨1点备份一次数据库,保存路径为宿主机的路径,而不是容器里面的。
mkdir -p /data/dockers/mysql/data/backup
contab -e
0 1 * * * docker exec mysql mysqldump -uroot -p123456 test> /data/dockers/mysql/data/backup/test-copy.sql
使用 navicate 远程连接成功。
如果远程连不上,需要开放3306端口