docker容器内外的操作

本文介绍如何使用Docker操作MySQL容器,包括进入容器、配置文件编辑、权限设置及基本SQL命令等关键步骤。

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

docker容器内外的操作

进入容器系统

进入 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

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

mysql配置文件my.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

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

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

chmod -R 777 /data/mysql
chmod -R 644 /data/mysql/conf/mysql

容器内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;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值