關(guān)于字符編碼錯(cuò)誤進(jìn)入mysql終端
mysql> show create database dbname; #查看創(chuàng)建庫(kù)時(shí)字符集
mysql> alter database dbname character set utf-8; #修改存在數(shù)據(jù)庫(kù)的字符集
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| study | CREATE DATABASE `study` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
mysql> show create table tablename; #查看創(chuàng)建表時(shí)字符集
mysql> alter table tablename default character set utf8; #修改存在表的字符集
| customers | CREATE TABLE `customers` (
`cust_id` int(11) NOT NULL AUTO_INCREMENT,
`cust_name` char(50) NOT NULL,
`cust_address` char(50) DEFAULT NULL,
`cust_city` char(50) DEFAULT NULL,
`cust_state` char(5) DEFAULT NULL,
`cust_zip` char(10) DEFAULT NULL,
`cust_country` char(50) DEFAULT NULL,
`cust_contact` char(50) DEFAULT NULL,
`cust_email` char(255) DEFAULT NULL,
PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=10006 DEFAULT CHARSET=latin1
mysql> SHOW FULL COLUMNS FROM tablename;#檢查數(shù)據(jù)表所有字段的狀態(tài)
mysql> alter table tablename change columnname columnname varchar(20) character set utf8 collate utf8_unicode_ci not null default '';#修改表中columnname的字符集
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------------+-----------+-------------------+------+-----+---------+----------------+---------------------------------+---------+
| cust_id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| cust_name | char(50) | latin1_swedish_ci | NO | | NULL | | select,insert,update,references | |
| cust_address | char(50) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_city | char(50) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_state | char(5) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_zip | char(10) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_country | char(50) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_contact | char(50) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references | |
| cust_email | char(255) | latin1_swedish_ci | YES | | NULL | | select,insert,update,references |
mysql> show variables like 'char%'; #查看數(shù)據(jù)庫(kù)使用的字符集
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin |
| character_set_connection | latin |
| character_set_database | latin |
| character_set_filesystem | binary |
| character_set_results | latin |
| character_set_server | latin |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)
mysql> set character_set_system=utf8;#修改系統(tǒng)默認(rèn)字符集
mysql> set character_set_results=utf8;#修改結(jié)果默認(rèn)字符集
mysql> set character_set_server=utf8;#修改服務(wù)器默認(rèn)字符集
mysql> set character_set_database=utf8;#修改數(shù)據(jù)庫(kù)字符集
mysql> set character_set_client=utf8;#修改數(shù)據(jù)庫(kù)客戶(hù)端字符集
mysql> set character_set_connection=utf8;#修改連接默認(rèn)字符集