2018再见!2019 你好!

MySQL_设置编码步骤

Mysql Txun 25℃ 0评论

1、查看mysql编码(默认latin1)

show variables like 'collation_%';
+--------------------------+----------------------------+
| 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 | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

2、临时修改编码(重启后失效)

mysql> alter database name character set utf8;
mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00 sec)

3、永久设置编码(修改配置文件,重启Mysqld服务)

win系统:
mysql目录下my.ini
[client]
port=3306
default-character-set=utf8
#客户端字符类型,与服务端一致就行,建议utf8
[mysqld]
port=3306
character_set_server=utf8
basedir=D:/MySql/mysql-5.6.17-winx64  #服务端字符类型,建议utf8
datadir=D:/MySql/mysql-5.6.17-winx64/data  #解压根目录
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[WinMySQLAdmin]
D:/MySql/mysql-5.6.17-winx64/bin/mysqld.exe  #解压根目录/bin/mysqld.exe
------------------------------------------------------------------------------------
linux凝思安全操作系统
$ vi /etc/my.cnf
[mysqld]
default-character-set=utf8
character_set_server=utf8
init_connect='SET NAMES utf8'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
[mysql]
default-character-set=utf8
[musql.server]
default-character-set=utf8
[client]
default-character-set=utf8
[mysqld_safe]
default-character-set=utf8
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

结果:

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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

4、创建MySql数据库和表并设置编码

CREATE TABLE IF NOT EXISTS test_tb( id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
或
CREATE TABLE IF NOT EXISTS test_tb( id int(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, name varchar(20) NOT NULL);
-- 修改表编码
ALTER TABLE  `test_tb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
--修改列编码 
ALTER TABLE  `test_tb` CHANGE  `name`  `name` VARCHAR( 100 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;

转载请注明:T讯脚印 » MySQL_设置编码步骤

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址