docker部署mysql服务-解决中文乱码-数据持久化

docker部署mysql服务-解决中文乱码-数据持久化

下载mysql5.7镜像

[root@localhost ~]# docker pull mysql:5.7
[root@localhost ~]# docker images
REPOSITORY    TAG       IMAGE ID       CREATED         SIZE
hello-world   latest    d2c94e258dcb   12 months ago   13.3kB
mysql         5.7       c20987f18b13   2 years ago     448MB

启动、运行

格式:宿主机端口3306,容器内端口3306,root密码123456,镜像版本mysql:5.7
-e是环境变量,指定mysql数据库密码

[root@localhost ~]# docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
84bbbc8705042184297e507d17186ae6a3b9fd1ba5b790150340e3ebc5712f56
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS        PORTS                                                  NAMES
84bbbc870504   mysql:5.7   "docker-entrypoint.s…"   2 seconds ago   Up 1 second   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   adoring_lamarr

#进入容器实例,登录数据库
[root@localhost ~]# docker exec -it adoring_lamarr /bin/bash
root@84bbbc870504:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

数据库操作

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database db01;
Query OK, 1 row affected (0.00 sec)

mysql> use db01;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql> insert into t1  values (1,'wq');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|    1 | wq   |
+------+------+
1 row in set (0.00 sec)

解决中文乱码、数据持久化

删除之前创建的容器

docker rm -f 84bbbc870504

重新创建一个容器

# 格式:
docker run -d -p 3306:3306 --privileged=True \
-v /docker-volume/mysql/log:/var/log/mysql \
-v /docker-volume/mysql/data:/var/lib/mysql \
-v /docker-volume/mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
--name=mysql mysql:5.7

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
4cd78afe58e9   mysql:5.7   "docker-entrypoint.s…"   14 seconds ago   Up 13 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
-v /docker-volume/mysql/log:/var/log/mysql   存放日志
-v /docker-volume/mysql/data:/var/lib/mysql    存放数据
-v /docker-volume/mysql/conf:/etc/mysql/conf.d    存放配置文件

解决中文乱码

进入同步的容器卷目录中,新建my.cnf文件,通过容器卷同步给mysql容器实例,解决中文乱码

[root@localhost ~]# cd /docker-volume/mysql/conf/
[root@localhost conf]# ll
total 0
[root@localhost conf]# vi my.cnf
[root@localhost conf]# cat my.cnf
[client]
default_character_set=utf8
[mysqld]
collation_server=utf8_general_ci
character_set_server=utf8

#重启容器实例生效
[root@localhost conf]# docker restart mysql
mysql

进入容器实例、登录数据库

[root@localhost conf]# docker exec -it mysql /bin/bash
root@4cd78afe58e9:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)


mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> use test;
Database changed

mysql> create table t1(id int,name varchar(20));
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t1 values (1,'Tom');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+------+
| id   | name |
+------+------+
|    1 | Tom  |
+------+------+
1 row in set (0.00 sec)

mysql> insert into t1 values (2,'测试');
Query OK, 1 row affected (0.00 sec)

mysql> select * from t1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | Tom    |
|    2 | 测试   |
+------+--------+
2 rows in set (0.00 sec)

检验数据持久化

mysql容器实例被删除之后,是否还能恢复

删除容器实例

[root@localhost conf]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
4cd78afe58e9   mysql:5.7   "docker-entrypoint.s…"   17 minutes ago   Up 10 minutes   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql
[root@localhost conf]# docker rm -f mysql
mysql
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

重新创建容器
指定卷

[root@localhost conf]# docker run -d -p 3306:3306 --privileged=True \
> -v /docker-volume/mysql/log:/var/log/mysql \
> -v /docker-volume/mysql/data:/var/lib/mysql \
> -v /docker-volume/mysql/conf:/etc/mysql/conf.d \
> -e MYSQL_ROOT_PASSWORD=123456 \
> --name=mysql mysql:5.7
0a16e25df22113c6ef3790fd2fb651edca9fb532de070704f3bcbf370cf10ac2
[root@localhost conf]# docker ps
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
0a16e25df221   mysql:5.7   "docker-entrypoint.s…"   22 seconds ago   Up 21 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   mysql

查看数据是否存在

[root@localhost conf]# docker exec -it mysql /bin/bash
root@0a16e25df221:/# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t1             |
+----------------+
1 row in set (0.00 sec)

mysql> select * from t1;
+------+--------+
| id   | name   |
+------+--------+
|    1 | Tom    |
|    2 | 测试   |
+------+--------+
2 rows in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/602747.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

1072 开学寄语(测试点2)

solution 测试点2:物品编号可能不足四位&#xff0c;高位需补0 #include<iostream> #include<string> using namespace std; const int maxn 1e5; int flag[maxn] {0}; int main(){int n, m, k, cnt 0, cnt1 0, have, x;string id;cin >> n >> m…

基于STM32的智能垃圾桶设计(论文+源码)_kaic

基于STM32的智能垃圾桶设计 摘 要 随着社会科学技术的迅猛进展&#xff0c;人们的生活质量和速度也在不断提高。然而&#xff0c;大多数传统的家庭垃圾桶已经过时且缺乏创新&#xff0c;缺乏人性化设计。它们使用起来不方便、不卫生&#xff0c;所有的生活和废物垃圾都被混合…

跨平台桌面客户端开发框架

跨平台桌面客户端开发框架允许开发者创建能够在多个操作系统上运行的桌面应用程序。以下是一些流行的跨平台桌面客户端开发框架。这些框架各有优势&#xff0c;选择哪个框架取决于项目需求、团队的技术栈以及对特定特性的偏好。 1.Electron &#xff1a; 使用JavaScript, HTML…

融知财经:期权期货及其他衍生产品

期权、期货及其他衍生产品是金融市场中用于管理风险和进行投机的金融工具。这些衍生产品的价值依赖于一个或多个基础资产&#xff0c;如股票、商品、利率、汇率等。以下是关于这些衍生产品的一些基本介绍&#xff1a; 1、期货&#xff08;Futures&#xff09;&#xff1a;期货是…

5月7号(信息差)

&#x1f30d;首次&#xff0c;西湖大学用蛋白质语言模型定向改造碱基编辑器&#xff0c;登Cell子刊 https://www.jiqizhixin.com/articles/2024-05-07-10 &#x1f384; 哈马斯宣布同意停火提议 https://finance.eastmoney.com/a/202405073067687785.html ✨ 中国将对…

数据结构学习:栈(详细讲解)

&#x1f381;个人主页&#xff1a;我们的五年 &#x1f50d;系列专栏&#xff1a;C语言基本概念 &#x1f337;追光的人&#xff0c;终会万丈光芒 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 目录 &#x1f697;1.对栈概念理解&#xff1a; &a…

【Debug日记】albumentations包安装失败解决方案

直接pip安装pip install albumentations 报错&#xff1a; ERROR: Command errored out with exit status 1:command: D:\anaconda3\envs\pytorch\python.exe D:\anaconda3\envs\pytorch\lib\site-packages\pip\_vendor\pep517\in_process\_in_process.py build_wheel C:\Users…

【编程题-错题集】连续子数组最大和(动态规划 - 线性 dp)

牛客对应题目链接&#xff1a;连续子数组最大和_牛客题霸_牛客网 (nowcoder.com) 一、分析题目 简单线性 dp。 1、状态表示 dp[i] 表示&#xff1a;以 i 位置为结尾的所有子数组中&#xff0c;最大和是多少。 2、状态转移方程 dp[i] max(dp[i - 1] arr[i], arr[i]) 3、返回…

Sarcasm detection论文解析 |使用 BERT 进行中间任务迁移学习的刺检测

论文地址 论文地址&#xff1a;https://www.mdpi.com/2227-7390/10/5/844#/ github&#xff1a;edosavini/TransferBertSarcasm (github.com) 论文首页 笔记框架 使用 BERT 进行中间任务迁移学习的讽刺检测 &#x1f4c5;出版年份:2022 &#x1f4d6;出版期刊:Mathematics &…

[C/C++] -- 装饰器模式

装饰器模式是一种结构型设计模式&#xff0c;它允许在不改变原始对象的基础上动态地扩展其功能。这种模式通过将对象包装在装饰器类的对象中来实现&#xff0c;每个装饰器对象都包含一个原始对象&#xff0c;并可以在调用原始对象的方法之前或之后执行一些额外的操作。 装饰器…

炫龙电脑数据恢复方法有哪些?4个常用方法大放送

随着科技的不断发展&#xff0c;电脑已成为我们日常生活中不可或缺的一部分。然而&#xff0c;无论是由于操作失误、病毒感染、系统崩溃还是硬件故障&#xff0c;数据丢失都可能是每个电脑用户都可能面临的问题。对于使用炫龙电脑的用户来说&#xff0c;了解并掌握一些基本的数…

webassembly入门详解(C++)

一、环境配置 环境说明,操作系统为window操作系统。 1.1 下载和安装python 下载 需要python版本至少3.6版本 python下载地址:https://www.python.org/getit/ 安装 检测安装结果 win+R组合键->cmd->输入python->回车 1.2 下载和安装emsdk 下载 下载地址:https://gi…

这个Python库Streamlit,5分钟内搭建可视化WEB应用

在数据科学的世界里&#xff0c;将分析结果快速、直观地呈现给非技术背景的决策者&#xff0c;是一项重要的技能。而Streamlit&#xff0c;这个开源的Python库&#xff0c;正是为此而生。它允许数据科学家和工程师通过少量的代码&#xff0c;快速创建和分享数据应用。今天&…

OpenAI推出DALL·E 3识别器、媒体管理器

5月8日&#xff0c;OpenAI在官网宣布&#xff0c;将推出面向其文生图模型DALLE 3 的内容识别器&#xff0c;以及一个媒体管理器。 随着ChatGPT、DALLE 3等生成式AI产品被大量应用在实际业务中&#xff0c;人们越来越难分辨AI和人类创建内容的区别&#xff0c;这个识别器可以帮…

【Git】Git学习-15:分支简介和基本操作

学习视频链接&#xff1a;【GeekHour】一小时Git教程_哔哩哔哩_bilibili​编辑https://www.bilibili.com/video/BV1HM411377j/?vd_source95dda35ac10d1ae6785cc7006f365780https://www.bilibili.com/video/BV1HM411377j/?vd_source95dda35ac10d1ae6785cc7006f365780 git bran…

pycharm code行太长显示波浪线取消

实际操作如下&#xff1a;个人比较合适的位置为160,180时有点多 效果&#xff1a;

Agent AI智能体:塑造未来社会的智慧力量

&#x1f525; 个人主页&#xff1a;空白诗 文章目录 &#x1f916; Agent AI智能体&#xff1a;塑造未来社会的智慧力量&#x1f3af; 引言&#x1f331; 智能体的未来角色预览&#x1f4bc; 行业革新者&#x1f31f; 创意合作者&#x1f6e1;️ 公共安全与环保&#x1f680; …

武汉星起航:亚马逊欧洲站:丰富商品与卓越服务铸就高客单价典范

亚马逊&#xff0c;作为全球最大的电商平台之一&#xff0c;其欧洲站在全球电商市场中占据着举足轻重的地位。其中&#xff0c;亚马逊欧洲站的人均客单价更是高居世界前列&#xff0c;这背后究竟隐藏着哪些原因呢&#xff1f; 首先&#xff0c;亚马逊具有丰富且高质量的商品品…

工业镜头助力锂电制造业精准检测

在电动汽车、电动轻型车、电动工具、消费电子和新型储能等行业大发展的背景下&#xff0c;锂电池综合优势与下游领域对电池大容量、高功率、使用寿命和环境保护日益提升的需求相契合&#xff0c;存在广阔的市场应用前景。受益于动力、消费和储能三大细分领域的快速发展&#xf…

Debug项目失败Run成功

一&#xff1a;问题 idea中启动服务&#xff0c;服务一直在启动中&#xff0c;最后超时启动失败 重新构建项目也是一样 二&#xff1a;个人分析 debug因为断点太多了项目起不起来&#xff0c;试一下run直接运行&#xff0c;项目可以快速启动 三&#xff1a;解决办法 在控制…