修改mysql编码为UTF-8

时间:2022-07-08 20:50:38
mysql> show variables  like '%character%';
+--------------------------+----------------------------+
| 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/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
修改数据库的字符集
  mysql>use mydb
  mysql>alter database mydb character set utf-8;

创建数据库指定数据库的字符集

  mysql>create database mydb character set utf-8;
 

通过配置文件修改:

1,service mysqld stop,停用mysql。

2.cp /etc/my.cnf /etc/my.cnf.bak,修改前做备份,这是个好习惯。

修改my.cnf或my.ini(只有修改配置文件,并重启服务器,才能永久生效)
vi /etc/my.cnf
在[client]下添加,client为控制客户端的,没试过,没有的可以不需要加。
default-character-set=utf8
在[mysqld]下添加,mysqld为控制服务器端的,改过了,OK。
default-character-set=utf8

character_set_server = utf8

3.service mysqld restart,重启。

4.show variables like '%char%';查看。

通过MySQL命令行修改:

mysql> set character_set_client=utf8;
Query OK, 0 rows affected (0.00
sec)

mysql> set character_set_connection=utf8;
Query OK, 0 rows affected (0.00
sec)

mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00
sec)

mysql> set character_set_results=utf8;
Query OK, 0 rows affected (0.00
sec)

mysql> set character_set_server=utf8;
Query OK, 0 rows affected (0.00
sec)

mysql> set character_set_system=utf8;
Query OK, 0 rows affected (0.01
sec)

mysql> set collation_connection=utf8;
Query OK, 0 rows affected (0.01
sec)

mysql> set collation_database=utf8;
Query OK, 0 rows affected (0.01
sec)

mysql> set collation_server=utf8;
Query OK, 0 rows affected (0.01
sec)