赞
踩
进入 mysql
容器
docker exec -it mysql /bin/bash
# 或
docker exec -it mysql /bin/sh
容器内的操作命令要看容器内运行的系统而定
在容器内登录mysql
mysql -u root -p
在容器外,宿主机使用mysql
客户端登录,需要加IP
地址:
mysql -h 127.0.0.1 -u root -p
操作完成之后, exit
命令退出容器
docker restart mysql # 重启mysql容器
docker stop mysql # 停止mysql容器
docker rm mysql # 删除mysql容器
docker rmi mysql:5.7 # 删除mysql:5.7镜像
在宿主机执行命令:
docker cp mysql:./etc/mysql/mysql.conf.d/mysqld.cnf /data/mysql/conf/mysqld.cnf
可把容器内的mysql
全局配置文件mysqld.cnf
复制到宿主机/data/mysql/conf/
路径下,配置文件修改完毕,再执行命令:
docker cp /data/mysql/conf/mysqld.cnf mysql:./etc/mysql/mysql.conf.d/mysqld.cnf
则可以把配置文件复制到容器内。
以上面示例路径为例
vim /data/mysql/conf/mysql/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 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/ # 自定义配置 tmpdir=/var/lib/mysql/tmp port=3306 # 默认时区设置为东8区 default-time_zone='+8:00' # 默认编码utf8mb4 character-set-server=utf8mb4 character-set-client-handshake=FALSE collation-server=utf8mb4_general_ci init_connect='SET NAMES utf8mb4' secure_file_priv=/var/lib/mysql default_authentication_plugin=mysql_native_password default-storage-engine=INNODB # 设置数据库大小写不敏感,根据实际项目需求设置,默认值是0 lower_case_table_names=1 # 最大连接数,默认设置是100 max_connections=800 # SQL数据包发送的大小,如果有BLOB对象建议修改成1G max_allowed_packet=128M # MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭 # MySQL默认的 wait_timeout 值为8个小时 # interactive_timeout 参数需要同时配置才能生效 interactive_timeout=28800 wait_timeout=28800 [mysql] no-auto-rehash default-character-set=utf8mb4 socket=/var/run/mysqld/mysqld.sock [client] default-character-set=utf8mb4 socket=/var/run/mysqld/mysqld.sock port=3306 [mysqld_safe] log-error=/logs/mysql.log pid-file=/var/run/mysqld/mysqld.pid socket=/var/run/mysqld/mysqld.sock [mysqldump] quick socket=/var/run/mysqld/mysqld.sock [mysqladmin] socket=/var/run/mysqld/mysqld.sock
如果容器映射宿主机数据路径,则需设置正确的权限,才可以正常启动
chmod -R 777 /data/mysql
chmod -R 644 /data/mysql/conf/mysql
-- docker创建mysql容器时候已经指定root密码,如果需要修改root密码,请参考: mysql>alter user 'root'@'localhost' identified by 'newpasswd'; Query OK, 0 rows affected (0.01 sec) -- 创建全域root用户(允许root从其他服务器访问) mysql> create user 'root'@'%' identified by 'newpasswd'; -- 进行授权 mysql> grant all privileges on *.* to 'root'@'%'; -- 刷新,使生效 mysql> flush privileges; mysql> grant all privileges on *.* to root@'%' with grant option; Query OK, 0 rows affected (0.01 sec) mysql> alter user 'root'@'%' identified by 'newpasswd' password expire never; Query OK, 0 rows affected (0.11 sec) mysql> alter user 'root'@'%' identified with mysql_native_password by 'newpasswd'; Query OK, 0 rows affected (0.11 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) -- 可选操作:创建新用户,并支持从其他服务器登录 mysql> create user 'dbauser'@'%' identified with mysql_native_password by 'password'; -- 授权 mysql> grant all privileges on *.* to 'dbauser'@'%'; -- 刷新,使生效 mysql> flush privileges; -- 查看执行结果 mysql> use mysql; mysql> select user,host,plugin from user; -- 查看 lower_case_table_names 参数设置 mysql> show variables like 'lower%'; -- 查看mysql当前时间,检查当前时区 mysql> select curtime(); -- 如果不是北京时间,则修改mysql全局时区为东8区 mysql> set global time_zone = '+8:00'; -- 修改当前会话时区 mysql> set time_zone = '+8:00'; -- 立即生效 mysql> flush privileges; -- 再次查看mysql当前时间,确认时区设置 mysql> select curtime(); -- 退出 mysql> exit;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。