本文介绍了命令行修改mysql字符编码的方法,安装mysql时,由于失误导致mysql默认编码为latin1,这样导致中文支持不了,遂想修改mysql编码为utf8。
1、修改数据库字符编码
复制代码 代码示例:
mysql> alter database mydb character set utf8 ;
2、创建数据库时,指定数据库的字符编码
复制代码 代码示例:
mysql> create database mydb character set utf8 ;
3、查看mysql数据库的字符编码
复制代码 代码示例:
mysql> show variables like 'character%'; //查询当前mysql数据库的所有属性的字符编码
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
4、修改mysql数据库的字符编码
复制代码 代码示例:
mysql> set character_set_client=utf8 ;
mysql> set character_set_connection=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_database=utf8 ;
mysql> set character_set_results=utf8 ;
mysql> set character_set_server=utf8 ;
mysql> set character_set_system=utf8 ;
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| 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数据库字符集编码
mysql版本:5.0.27
安装mysql时,由于失误导致mysql默认编码为latin1,这样导致中文支持不了,遂想修改mysql编码为utf8。
步骤如下:
一、管理工具-->服务-->停掉mysql服务
二、修改my.ini配置文件:
1.在[client]的port=3306的下面一行添加default-character-set=utf8
2.将[mysql]中的default-character-set=latin1中的latin1改为utf8
3. 将[mysqld]中的default-character-set=latin1中的latin1改为utf8
三、重启mysql服务
service mysqld restart
四、进入mysql命令行,输入以下命令检查
复制代码 代码示例:
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)
mysql> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+