当前位置:   article > 正文

docker容器内外的操作_docker 容器内的mysql,访问容器外的文件

docker 容器内的mysql,访问容器外的文件

docker容器内外的操作

进入容器系统

进入 mysql 容器

docker exec -it mysql /bin/bash
# 或
docker exec -it mysql /bin/sh
  • 1
  • 2
  • 3

容器内的操作命令要看容器内运行的系统而定

在容器内登录mysql

mysql -u root -p
  • 1

在容器外,宿主机使用mysql客户端登录,需要加IP地址:

mysql -h 127.0.0.1 -u root -p
  • 1

操作完成之后, exit 命令退出容器

容器控制

docker restart mysql # 重启mysql容器
docker stop mysql # 停止mysql容器
docker rm mysql # 删除mysql容器
docker rmi mysql:5.7 # 删除mysql:5.7镜像
  • 1
  • 2
  • 3
  • 4

容器内外文件传递(参考)

在宿主机执行命令:

docker cp mysql:./etc/mysql/mysql.conf.d/mysqld.cnf /data/mysql/conf/mysqld.cnf
  • 1

可把容器内的mysql全局配置文件mysqld.cnf复制到宿主机/data/mysql/conf/路径下,配置文件修改完毕,再执行命令:

docker cp /data/mysql/conf/mysqld.cnf mysql:./etc/mysql/mysql.conf.d/mysqld.cnf
  • 1

则可以把配置文件复制到容器内。

mysql配置文件my.cnf(参考)

以上面示例路径为例

vim /data/mysql/conf/mysql/my.cnf
  • 1
# 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80

mysql数据配置与数据目录权限(参考)

如果容器映射宿主机数据路径,则需设置正确的权限,才可以正常启动

chmod -R 777 /data/mysql
chmod -R 644 /data/mysql/conf/mysql
  • 1
  • 2

容器内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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/284258
推荐阅读
相关标签
  

闽ICP备14008679号