当前位置:网站首页>mysql 执行脚本的正确姿势
mysql 执行脚本的正确姿势
2022-06-22 11:55:00 【thlzjfefe】
一般我们会将mysql部署在linux服务器上,而在执行脚本时,又无法通过SQLyog等客户端工具进行连接,如果要执行脚本,只能通过命令行的方式执行。
此时如果我们简单的把脚本复制过去执行,可能会出现中文乱码的问题,这是因为mysql的客户端默认使用的字符集是latin1,而其它客户端如SQLyog、java程序等一般会使用utf8的方式。为避免乱码的问题,我们可以通过以下两种方式执行sql脚本:
查看mysql的字符集
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
可见,character_set_client、character_set_connection、character_set_results默认使用的字符集是latin1
一、通过souce命令
1、连接mysql
mysql -uroot -p123456 test
2、设置此客户端的字符集
set names utf8;
3、执行脚本
source /var/data/1.sql
二、修改客户端默认字符集后,再执行
1、在/etc/mysql/mysql.conf.d/mysqld.cnf配置文件中添加:default-character-set=utf8
[[email protected]]# cat mysql.cnf
[mysql]
default-character-set=utf8
再查看字符集,已修改为了utf8
mysql> show variables like 'char%';
+--------------------------+----------------------------+
| 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.01 sec)
2、重启mysql
3、执行脚本
mysql -uroot -p123456 test < 1.sql注意:此种方式可能数据库会被删除,一定要备份,在数据库安装之处使用,切记!!!!!!!
边栏推荐
- Redis - 4. Three new data types
- Linux Installation and deployment mysql5.7 (enterprise common Edition) ultra detailed
- Redis - 4、新的3种数据类型
- chown改变目录的所有者、所属组失败的诡异问题
- Fight, programmer chapter 39 how can you admire a bosom friend if you are not disappointed in life
- TIS教程02-模型
- MAUI使用Masa blazor组件库
- Tensorflow2 模型优化环境快速搭建
- Vue混写Minxin、虚拟Dom/ref、动态组件、缓存keep
- C语言itoa()函数和atoi()函数详解(整数转字符C实现)
猜你喜欢

SAP Marketing Cloud 功能概述(二)

In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions
![[cloud native | kubernetes] Introduction to kubernetes (I)](/img/e0/c75e72fc3636b3de8f5caded19a883.png)
[cloud native | kubernetes] Introduction to kubernetes (I)

Differences between SPI and API

SPI 与 API的区别

What is homology??? Cross domain error??? How to solve???

Authenticated cookies, sessions, JWT

Reddit产品主管:为Web3创作者准备的NFT会员实用指南

初始transformer需要了解的一些概念

Redis - 9. Persistent AOF (append onlyfile)
随机推荐
Latex希腊字母对照表
求100-200之间全部的素数
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
New progress in the construction of meituan's Flink based real-time data warehouse platform
本地裸文件包含
Promise upgraded async, await is coming, which is more fragrant with try+catch
Fight, programmer chapter 39 how can you admire a bosom friend if you are not disappointed in life
Nansen年度报告
Linux安装部署MySQL5.7(企业常用版)超详细
重磅直播|BizDevOps:数字化转型浪潮下的技术破局之路
SQL Server到Oracle连接服务器的实现_包括查询的实现
Overview of SAP marketing cloud functions (II)
Some concepts that the initial transformer needs to understand
12306抢票使用教程
论文解读——Scalable Collaborative Filtering with Jointly Derived Neighborhood Interpolation Weights
Messari年度报告-2021
The input input box can only input an array between 0 and 100, with two decimal places reserved
隐形交通指挥员:浅述城市轨交信号系统
Struggle, programmer chapter 38 in the old days, when the road turned to the edge of the forest, I suddenly saw
函数指针和指针函数的区别