linux 设置mysql 数据库编码utf8
UTF8: CREATE DATABASE `test2` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci
?
?
轉載地址
http://www.linuxidc.com/Linux/2015-08/121676.htm
?
在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
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:? ?24 min 14 sec
Threads: 5? Questions: 501? Slow queries: 0? Opens: 144? Flush tables: 1? Open tables: 129? Queries per second avg: 0.344
--------------
mysql>
MySQL修改默認字符集為utf-8? http://www.linuxidc.com/Linux/2015-08/121653.htm
本文永久更新鏈接地址:http://www.linuxidc.com/Linux/2015-08/121676.htm
轉載于:https://www.cnblogs.com/gzyx1988/p/6073053.html
總結
以上是生活随笔為你收集整理的linux 设置mysql 数据库编码utf8的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全局异步和主线程异步区别、改变Place
- 下一篇: Codeforces Round #37