1,承接上一随笔,因为我用的是rmp的两种反式。
rpm -ivh MySQL-server-4.0.14-0.i386.rpm
rpm -ivh MySQL-client-4.0.14-0.i386.rpm
我发现在我的linux下/etc/目录下面没有我my.cnf文件。这听说是我用了这种方式安装mysql的问题
于是:
[root@iZ945sgm0ugZ etc]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
并在my.cnf的[client]下加了一行default-character-set=utf8
[client]
#password = your_password
default-character-set=utf8
port =
socket = /var/lib/mysql/mysql.sock
且我还在[mysqld]下面加character-set-server=utf8
[mysqld]
character-set-server=utf8
重新启动:
[root@iZ945sgm0ugZ etc]# /etc/init.d/mysql stop
Shutting down MySQL... [ OK ]
[root@iZ945sgm0ugZ etc]# /etc/init.d/mysql start
Starting MySQL.. [ OK ]
乱码问题解决,ok
2,通过mysql的sql指令来设置
1,开启mysql服务,
2,输入指令,查看当前编码
mysql> show variables like 'collation_%';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.00 sec) mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| 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> set character_set_database=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_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> show variables like 'collation_%';
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec) 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)