docker安装mysql57镜像

本文详细介绍了如何使用Docker部署MySQL数据库,包括选择合适的MySQL版本、配置数据持久化、网络设置、端口映射及定时备份策略。通过具体步骤和命令,帮助读者快速掌握Docker环境下MySQL的安装与配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考 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 ps

netstat -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端口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值