CentOS下配置MySQL的编码:
0.安装mysql:sudo yum install mysql mysql-server mysql-devel
1.启动服务: sudo /etc/rc.d/init.d/mysqld start
2.修改root密码: mysqladmin -uroot password 'yourpassword'
3.修改启动文件: sudo /etc/rc.d/init.d/mysqld :
CentOS 5 自带的 Mysql5.0.22里是此处,添加 --default-character-set=utf8 :
/usr/bin/mysqld_safe --defaults-file=/etc/my.cnf --pid-file="$mypidfile" --log-error="$errlogfile" >/dev/null --default-character-set=utf8 2>&1 &
CentOS5.2里自带的Mysql5.0.45里是此处, 添加 --default-character-set=utf8 :
/usr/bin/mysqld_safe --datadir="$datadir" --socket="$socketfile" \
--log-error="$errlogfile" --pid-file="$mypidfile" \
>/dev/null --default-character-set=utf8 2>&1 &
OpenSuse11.3的Mysql-5.1.46-log SUSE MySQL RPM是在大约259行,添加default_character_set=utf8 :
pid_file=/var/run/mysql/mysqld.pid
socket=/var/run/mysql/mysql.sock
print_defaults=/usr/bin/my_print_defaults
log_error=/var/log/mysql/mysqld.log
default_character_set=utf8
mode=$1 # start or stop
log_base="`echo "$log_error" | sed 's|\.log$||'`"
CentOS 6.1 自带的MySQL 5.1.52-1, 是在大约 第99行,添加 --character-set-server=utf8 :
$exec --datadir="$datadir" --socket="$socketfile" \
--pid-file="$mypidfile" \
--basedir=/usr --user=mysql >/dev/null --character-set-server=utf8 2>&1 &
注意这里用的是 --character-set-server=utf8 。
4.修改my.conf
[ mysqld]
max_allowed_packet = 2M
max_connections = 1000
default-character-set=utf8 (注意:在CentOS 6.x 里, 这里 应该是 character-set-server =utf8)
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8
5.重启服务:sudo /etc/init.d/mysqld restart
如果需要mysql服务随系统自动启动,需要运行sudo /sbin/chkconfig mysqld --level 345 on
6.测试结果:(如,centos5.2 下结果)
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
smysql> status;
--------------
mysql Ver 14.12 Distrib 5.0.45, for redhat-linux-gnu (i686) using readline 5.0
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.0.45 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 15 sec
Threads: 1 Questions: 4 Slow queries: 0 Opens: 12 Flush tables: 1 Open tables: 6 Queries per second avg: 0.267
--------------
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.01 sec)
附:在CentOS 6.0 里的操作时控制台输出的记录:
[simon_sun@gnu ~]$ sudo /etc/rc.d/init.d/mysqld start
Initializing MySQL database: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h gnu.linux password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
Starting mysqld: [ OK ]
[simon_sun@gnu ~]$ /usr/bin/mysqladmin -u root password 'MyPasswd'
[simon_sun@gnu ~]$ sudo vim /etc/rc.d/init.d/mysqld
[simon_sun@gnu ~]$ sudo vim /etc/my.cnf
[simon_sun@gnu ~]$ sudo /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
[simon_sun@gnu ~]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.52 Source distribution
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> status
--------------
mysql Ver 14.14 Distrib 5.1.52, for unknown-linux-gnu (x86_64) using readline 5.1
Connection id: 2
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.1.52 Source distribution
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 18 sec
Threads: 1 Questions: 6 Slow queries: 0 Opens: 15 Flush tables: 1 Open tables: 8 Queries per second avg: 0.333
--------------
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> quit
Bye
7.以上在
CentOS4.6, mysql-5.0.27;
CentOS5.4, mysql 5.0.77;
CentOS5.2, mysql-5.0.45;
CentOS5.0, mysql-5.0.22;
OpenSuse11.3, Server version: 5.1.46-log SUSE MySQL RPM;
CentOS 6.0, mysql-5.1.52;
CentOS 6.1, mysql-5.1.52-1 上均测试通过。
[simon_sun@gnu ~]$ cat /etc/issue
CentOS Linux release 6.1 (Final)
Kernel \r on an \m
[simon_sun@gnu ~]$ uname -ar
Linux gnu.linux 2.6.32-131.21.1.el6.x86_64 #1 SMP Tue Nov 22 19:49:27 BST 2011 x86_64 x86_64 x86_64 GNU/Linux