设为首页 加入收藏

TOP

MySQL数据库编码集设置为utf8(一)
2015-08-31 19:59:34 来源: 作者: 【 】 浏览:95
Tags:MySQL 数据库 编码 设置 utf8

在MySQL数据库中, 当插入数据无法支持中文时, 可将该数据库的编码集设置为utf8, 故在创建数据库时, 将数据库编码集及其他字符编码设置为utf8。


如果之前创建的数据库的数据库编码集不为utf8, 可以进行如下设置:


mysql> alter database 数据库名 character set utf8;


或删除原有旧的数据库, 再重新创建新的数据库并将默认的编码集设置为utf8:


mysql> create database 数据库名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


* 删除数据库:


mysql> drop database dbname if exists dbname;


* 创建数据库:


mysql> create database dbname if not exists dbname;


为确保 create database dbname 命令创建的数据库编码集为utf8, 我们可对mysql的配置文件进行修改windows下的mysql配置文件为my.ini, my.ini存在于mysql的安装目录下linux下的mysql配置文件为my.cnf, mysql.cnf存在于mysql的安装目录下或/etc/下在my.cnf或my.ini下找到[mysqld], 在其下方添加一行:


character_set_server=utf8


然后保存退出, 并重启mysql服务即可。


查看字符编码:


mysql> show variables like '%character%';



mysql> show variables like 'character_set_%';



mysql> use 数据库名;


mysql> \s



mysql> use 数据库名


mysql> status;


具体操作如下:


mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------------+
| Variable_name? ? ? ? ? ? | Value? ? ? ? ? ? ? ? ? ? ? ? ? ? |
+--------------------------+----------------------------------+
| character_set_client? ? | utf8? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_connection | utf8? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_database? | latin1? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_filesystem | binary? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_results? ? | utf8? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_server? ? | latin1? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_set_system? ? | utf8? ? ? ? ? ? ? ? ? ? ? ? ? ? |
| character_sets_dir? ? ? | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)



mysql> set character_set_database = utf8;
Query OK, 0 rows affected (0.00 sec)



mysql> set character_set_server = utf8;
Query OK, 0 rows affected (0.00 sec)



mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------------+
| 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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)



mysql> use db_ets;
Database changed
mysql> \s
--------------
mysql? Ver 14.14 Distrib 5.7.5-m15, for linux-glibc2.5 (x86_64) using? EditLine wrapper



Connection id:? 16
Current database:?db_ets
Current user:? root@localhost
SSL:? ?Not in use
Current pager:? stdout
Using outfile:? ''
Using delimiter:?;
Server version:? 5.7.5-m15 MySQL Community Server (GPL)
Protocol version:?10
Connection:? Localhost via UNIX socket
Server characterset:?utf8
Db? ? characterset:?utf8
Client characterset:?utf8
Conn.? characterset:?utf8
UNIX socket:? /tmp/mysql.sock
Uptime:? ?23 min 45 sec



Threads: 3? Questions: 487? Slow queries: 0? Opens: 144? Flush tables: 1? Open tables: 129? Queries per second avg: 0.341
--------------



mysql> status;
--------------
mysql? Ver 14.14 Distrib 5.7.5-m15, for linux-glibc2.5 (x86_64) using? EditLine wrapper



Connection id:? 16
Current database:?db_ets
Current user:? root@localhost
SSL:? ?Not in use
Current pager:? stdout
Us

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇MongoDB 的基本操作 下一篇Oracle表连接——处理连接过程中..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: