docker拉取mysql镜像并导入sql文件执行
#拉取镜像
$ docker pull mysql:5.7
#查看镜像
$ docker images
#运行docker mysql密码123456 宿主机端口号3306 映射 容器端口号3306
$ docker run -itd --name mysql-test -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7
#查看进程 可以找到运行的容器id docker ps -a 查看所有容器,包含未运行状态容器
$ docker ps
#进入容器 exec方式进入容器后通过exit退出容器时,不会关闭容器
$ docker exec -it 95a6dd388742 bash
#退出容器
$ exit
#删除容器 容器id
$ docker rm -f ca1172e3788d
#删除镜像 镜像id
$ docker rmi -f 9b9cb95443b5
#宿主机sql文件复制进docker [cp 宿主机文件路径 容器id:容器内路径] 容器内路径必须是已存在的路径
$ docker cp /Users/v_weishan/Downloads/mysql.sql 95a6dd388742:/import/
#进入容器后
#进入数据库
$ mysql -h localhost -u root -p123456
#新建数据库
$ create database test;
#进入数据库
$ use test;
#执行导入的sql文件 [source sql文件路径]
$ source /import/mysql.sql
docker安装vim
#同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包
$ apt-get update
#安装
$ apt-get install vim
mysql操作,下述创建语句中包含有对时间字段默认赋值0000-00-00 00:00:00
因为mysql配置,会导致创建语句执行不成功,涉及到的配置为sql_mode。
可通过修改mysqld.cnf(修改之后需重启容器生效,时效永久)或者@@sql_mode(只对当前连接生效,重新连接数据库失效)
连接mysql修改@@sql_mode
// 导入创建语句执行 `create_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '' 时报错 ERROR 1067 (42000): Invalid default value for 'create_time'
//查看数据库配置
SHOW VARIABLES LIKE 'sql_mode%';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.27 sec)
//NO_ZERO_IN_DATE,NO_ZERO_DATE影响日期默认值
//不重启mysql的情况下设置。临时生效,重新连接mysql后失效
set sql_mode=(select replace(@@sql_mode,'NO_ZERO_IN_DATE,NO_ZERO_DATE',''));
修改mysqld.cnf
# 增加sql_mode配置,原配置文件中无此配置,可从数据库中查询后去掉NO_ZERO_IN_DATE,NO_ZERO_DATE后,配置到配置文件中
$ vi /etc/mysql/mysql.conf.d/mysqld.cnf
$ cat /etc/mysql/mysql.conf.d/mysqld.cnf
# Copyright (c) 2014, 2021, Oracle and/or its affiliates.
#
# 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
#
# The MySQL Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is