docker学习笔记(三) ——dockers启动MySQL并挂载文件
一、docker命令
1.引入库
docker pull mysql
2.my.cnf默认配置文件
# Copyright (c) 2017, 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 as published by
# the Free Software Foundation; version 2 of the License.
#
# 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 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
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
3.容器挂载命令
docker run --restart=always --privileged=true -d -v /dockerImageFile/mysql/data:/var/lib/mysql -v /dockerImageFile/mysql/conf:/etc/mysql/conf.d -v /dockerImageFile/mysql/my.cnf:/etc/mysql/my.cnf -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql --lower_case_table_names=1
--restart=always 代表开启启动
--privileged=true 代表进入容器内部为管理员身份
-d 表示后台运行容器 并返回容器Id
-v 把mysql产生的数据同步到本地 防止数据丢失
-e 容器传参 设置mysql的初始密码
--lower_case_table_names=1 设置数据库不严格区分大小写
docker ps
二. 设置mysql可远程访问
1.进入容器内
docker exec -it mysql01(容器id或者名字) /bin/bash
2.内部连接mysql
mysql -uroot -p123456
-u 后面连接用户名 -p后面连接密码
3. 具体命令
查询数据库连接空间
show databases;
设置远程连接用户
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
退出容器重启容器
-- mysql为容器名字
docker restart mysql
用navicat连接
三.如果在创建时容器没有设忽略大小设置
- 如果配置my.cnf的映射配置,则修改my.cnf配置文件即可
在[mysqld]下添加如下:lower_case_table_names=1
lower_case_table_names=1
四.创建函数问题
如果出现
This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 提示问题
这是我们开启了bin-log, 我们就必须指定我们的函数是否是
1 DETERMINISTIC 不确定的
2 NO SQL 没有SQl语句,当然也不会修改数据
3 READS SQL DATA 只是读取数据,当然也不会修改数据
4 MODIFIES SQL DATA 要修改数据
5 CONTAINS SQL 包含了SQL语句
其中在function里面,只有 DETERMINISTIC, NO SQL 和 READS SQL DATA 被支持。如果我们开启了
bin-log, 我们就必须为我们的function指定一个参数。
在MySQL中创建函数时出现这种错误的解决方法:
set global log_bin_trust_function_creators=TRUE;