MySQL查看与修改编码方式(mysql、数据库、表)
mysql默認的編碼方式是latin1,通過以下命令查看
show variables like 'char%';或者:
show variables like 'character%';
mysql> show variables like "character%";
+--------------------------+----------------------------+
| Variable_name ? ? ? ? ? ?| Value ? ? ? ? ? ? ? ? ? ? ?|
+--------------------------+----------------------------+
| character_set_client ? ? |?latin1? ? ? ? ? ? ? ? ? ? ? |
| character_set_connection | latin1 ? ? ? ? ? ? ? ? ? ? ? |
| character_set_database ? | latin1 ? ? ? ? ? ? ? ? ? ? |
| character_set_filesystem | binary ? ? ? ? ? ? ? ? ? ? |
| character_set_results ? ?| latin1? ? ? ? ? ? ? ? ? ? ? |
| character_set_server ? ? | latin1 ? ? ? ? ? ? ? ? ? ? |
| character_set_system ? ? |?latin1? ? ? ? ? ? ? ? ? ? ? |
| character_sets_dir ? ? ? | /opt/lampp/share/charsets/ |
+--------------------------+----------------------------+
從以上信息可知數(shù)據(jù)庫的編碼為latin1,需要修改為gbk或者是utf8;
其中,character_set_client為客戶端編碼方式;
character_set_connection為建立連接使用的編碼;
character_set_database數(shù)據(jù)庫的編碼;
character_set_results結(jié)果集的編碼;
character_set_server數(shù)據(jù)庫服務器的編碼;
修改mysql默認編碼方式:
修改mysql的配置文件my.ini,該文件目錄一般為/etc/my.ini
找到客戶端配置[client]?在下面添加?
default-character-set=utf8?默認字符集為utf8?
在找到[mysqld]?添加?
default-character-set=utf8?默認字符集為utf8?
init_connect='SET NAMES utf8'?(設定連接mysql數(shù)據(jù)庫時使用utf8編碼,以讓mysql數(shù)據(jù)庫為utf8運行)?
修改后大致如下:
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-character-set=utf8 init_connect='SET NAMES utf8' character_set_server=utf8 user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0[mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid[client] default-character-set=utf8 修改好后,重新啟動mysql?即可,重新查詢數(shù)據(jù)庫編碼可發(fā)現(xiàn)編碼方式的改變:
show variables like 'char%';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.00 sec)
查看mysql數(shù)據(jù)庫編碼方式:
show create database mydatabase;
show create database redmine;
+----------+------------------------------------------------------------------+
| Database | Create Database ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+----------+------------------------------------------------------------------+
| redmine ?| CREATE DATABASE `redmine` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
從以上看出數(shù)據(jù)庫的默認編碼方式為latin1
修改mysql數(shù)據(jù)庫編碼方式:
alter database mydatabase character set utf8;show create database redmine; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
+----------+------------------------------------------------------------------+
| Database | Create Database ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?|
+----------+------------------------------------------------------------------+
| redmine ?| CREATE DATABASE `redmine` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
可以看到數(shù)據(jù)庫的編碼方式變?yōu)榱藆tf8
查看mysql數(shù)據(jù)表編碼方式
show create table mydatabase;
| projects | CREATE TABLE `projects` (
? `id` int(11) NOT NULL AUTO_INCREMENT,
? `name` varchar(255) NOT NULL DEFAULT '',
? `description` text,
? `homepage` varchar(255) DEFAULT '',
? `is_public` tinyint(1) NOT NULL DEFAULT '1',
? `parent_id` int(11) DEFAULT NULL,
? `created_on` datetime DEFAULT NULL,
? `updated_on` datetime DEFAULT NULL,
? `identifier` varchar(255) DEFAULT NULL,
? `status` int(11) NOT NULL DEFAULT '1',
? `lft` int(11) DEFAULT NULL,
? `rgt` int(11) DEFAULT NULL,
? `inherit_members` tinyint(1) NOT NULL DEFAULT '0',
? PRIMARY KEY (`id`),
? KEY `index_projects_on_lft` (`lft`),
? KEY `index_projects_on_rgt` (`rgt`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 |
可以看到projects表的編碼方式為latin1
修改mysql數(shù)據(jù)表編碼方式
alter table projects convert to character set utf8 collate utf8_general_ci;
以上projects為我要查看的表
| projects | CREATE TABLE `projects` (
? `id` int(11) NOT NULL AUTO_INCREMENT,? `name` varchar(255) NOT NULL DEFAULT '',
? `description` text,
? `homepage` varchar(255) DEFAULT '',
? `is_public` tinyint(1) NOT NULL DEFAULT '1',
? `parent_id` int(11) DEFAULT NULL,
? `created_on` datetime DEFAULT NULL,
? `updated_on` datetime DEFAULT NULL,
? `identifier` varchar(255) DEFAULT NULL,
? `status` int(11) NOT NULL DEFAULT '1',
? `lft` int(11) DEFAULT NULL,
? `rgt` int(11) DEFAULT NULL,
? `inherit_members` tinyint(1) NOT NULL DEFAULT '0',
? PRIMARY KEY (`id`),
? KEY `index_projects_on_lft` (`lft`),
? KEY `index_projects_on_rgt` (`rgt`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 |
此時projects表的編碼方式已經(jīng)變?yōu)閡tf8
參考地址:
http://blog.csdn.net/frinder/article/details/7041723
http://www.jbxue.com/db/18042.html
http://bbs.csdn.net/topics/390728070?page=1
總結(jié)
以上是生活随笔為你收集整理的MySQL查看与修改编码方式(mysql、数据库、表)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IP选路与动态选路协议(六)
- 下一篇: linux下一款好用的命令行浏览器