mysql数据库字符集问题,查看,删除,连接数据库
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
mysql数据库字符集问题
查看建表语句
mysql> show create database oldboy_default\G
*************************** 1. row ***************************
Database: oldboy_default
Create Database: CREATE DATABASE `oldboy_default` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)
其中latin1 */为字符集,默认的字符集
建立一个GBK字符集的数据库
mysql> create database oldboy_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 1 row affected (0.00 sec)
查看一下字符集
mysql> show create database oldboy_gbk\G;
*************************** 1. row ***************************
Database: oldboy_gbk
Create Database: CREATE DATABASE `oldboy_gbk` /*!40100 DEFAULT CHARACTER SET gbk */
1 row in set (0.00 sec)
ERROR:
No query specified
发现字符集变成gbk */了
创建utf8字符集的库
mysql> create database oldboy_utf8 default character set utf8 collate utf8_general_ci;
其中collate utf8_general_ci; 是校对规则
mysql> show character set;
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
+----------+-----------------------------+---------------------+--------+
39 rows in set (0.00 sec)
Query OK, 1 row affected (0.00 sec)
士可杀不可辱
mysql> show create database oldboy_utf8\G;
*************************** 1. row ***************************
Database: oldboy_utf8
Create Database: CREATE DATABASE `oldboy_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */
1 row in set (0.00 sec)
ERROR:
No query specified
发现字符集变成utf8 */了
字符集不一致是数据库中文内容乱码的罪魁祸首,如果编译的时候指定了特定的字符集,则以后创建数据库就不需要指定字符集了
企业里创建数据库要看开发的程序确定字符集utf8的比较多
确定字符集后编译的时候指定字符集,--DDEFAULT_CHARSET=utf8
--DDEFAULT_COLLATION=utf8_general_ci
建库时不用指定字符集默认即可 如果编译时指定字符集有错如何解决
指定字符集创建数据库即可 数据库编译时要把这些字符集支持编译进去
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii
连接数据库
mysql> use oldboy_utf8
Database changed
查看当前数据库
mysql> select database();
+-------------+
| database() |
+-------------+
| oldboy_utf8 |
+-------------+
1 row in set (0.00 sec)
删除数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| oldboy_default |
| oldboy_gbk |
| oldboy_utf8 |
| performance_schema |
| test |
+--------------------+
7 rows in set (0.00 sec)
mysql> drop database oldboy_default;
Query OK, 0 rows affected (0.04 sec)
mysql> show databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql |
| oldboy_gbk |
| oldboy_utf8 |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
一些常用函数使用
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.32 |
+-----------+
1 row in set (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2016-03-15 00:39:53 |
+---------------------+
1 row in set (0.00 sec)
删除用户
mysql> select user,host from er;
+---------+-----------------------+
| user | host |
+---------+-----------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| zhanggy | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
+---------+-----------------------+
7 rows in set (0.00 sec)
mysql> drop user ""@'localhost';
Query OK, 0 rows affected (0.00 sec)
修改字符集的命令
alter database character set ***
alter table tablename character set ***
对新数据有效对已存在数据无效
已经有记录的字符集的调整,必须先将数据导出,经过修改字符集后重新导入后才可完成。
对已经有记录的字符集数据的修改步骤 latin1字符集数据库改为utf8数据库
1,导出表结构
mysqldump -uroot -poracle -p --default-character-set=latin1 -d dbname >alltable.sql
--default-character-set=latin1 表示以latin1字符集连接
-d 只导表结构
2,编辑altable.sql 将latin1 改成utf8
3,确保数据库不更新,导出所有数据
mysqldump -uroot -poracle --quick --no-create-info --extended-insert --default-character-set=latin1 oldboy>/opt/alldata.sql
--quick 用于转储大表 强制从服务器一次一行的检索数据而不是检索所有行并输出前cache到内存中
--no-create-info 不创建create table语句
--extended-insert 使用包括几个values列表的多行insert语法,这样文件更小io也小。
--default-character-set =latin1按照原有字符集导出数据,这样导出的文件中所有中文都是可见的不是乱码
4,打开alltable.sql 将所有latin1改成utf8
5,重建数据库
create database dbname default charset utf8
6,创建表 执行alltable.sql
7,导入数据,执行alldata.sql