备注:配置MySQL数据库字符集的目的是方便的使用数据库,无需在每次连接的时候都要临时设置数据库字符集的,个人不建议采用这种方法,真正的工程项目都应该在连接数据库时临时设置数据库字符集,如此才便于系统的移植,而且又不会影响数据库服务器中的其他数据库的使用!
安装完成之后,需要配置MySQL的字符集配置,首先需要查找MySQL的配置文件的位置,由于MySQL的配置文件名是以.cnf结尾的,因此可用如下命令进行查找:
- [root@localhost ~]# find / -iname ‘*.cnf‘ -print
- /usr/share/mysql/my-large.cnf
- /usr/share/mysql/my-medium.cnf
- /usr/share/mysql/my-innodb-heavy-4G.cnf
- /usr/share/mysql/my-huge.cnf
- /usr/share/mysql/my-small.cnf
- /usr/share/doc/MySQL-server-community-5.1.56/my-large.cnf
- /usr/share/doc/MySQL-server-community-5.1.56/my-medium.cnf
- /usr/share/doc/MySQL-server-community-5.1.56/my-innodb-heavy-4G.cnf
- /usr/share/doc/MySQL-server-community-5.1.56/my-huge.cnf
- /usr/share/doc/MySQL-server-community-5.1.56/my-small.cnf
- /etc/pki/tls/openssl.cnf
输入完命令“find / -iname ‘*.cnf‘-print”回车后,屏幕便显示搜索到的MySQL配置文件,然后拷贝my-large.cnf、my-medium.cnf 、my-innodb-heavy-4G.cnf 、my-huge.cnf、my-small.cnf中任意的一个到/etc目录下,并命名为my.cnf,其命令如下所示:
- [root@localhost ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
- [root@localhost ~]# vi /etc/my.cnf
然后,使用vi编辑器修改/etc/my.cnf文件,在[client]下添加: “default-character-set=gb2312”;在[mysqld]下添加:“default-character-set=gb2312”。如下所示:
- # The following options will be passed to all MySQL clients
- [client]
- default-character-set=gb2312
- #password = your_password
- port = 3306
- socket = /var/lib/mysql/mysql.sock
-
- # Here follows entries for some specific programs
-
- # The MySQL server
- [mysqld]
- default-character-set=gb2312
- port = 3306
- socket = /var/lib/mysql/mysql.sock
- skip-locking
- key_buffer_size = 16M
- max_allowed_packet = 1M
- table_open_cache = 64
- sort_buffer_size = 512K
- net_buffer_length = 8K
按一下Esc键,输入“:wq”后回车保存配置文件,输入“/etc/rc.d/init.d/mysqlrestart”重启MySQL服务,如下所示:
- [root@localhost ~]# /etc/rc.d/init.d/mysql restart
- Shutting down MySQL..[确定]
- Starting MySQL..[确定]
最后,我们来验证MySQL服务器配置是否成功,首先登录MySQL,输入“mysql –uroot -p”回车,系统提示输入密码,登录成功后进入MySQL命令模式,如下所示:
- [root@localhost ~]# mysql -uroot -p
- Enter password:
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 2
- Server version: 5.1.56-community-log MySQL Community Server (GPL)
- Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
- Oracle is a registered trademark of Oracle Corporation and/or its
- affiliates. Other names may be trademarks of their respective
- owners.
- Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.
- mysql>
在MySQL命令模式下分别输入“show variables like‘collation_%‘;”、“show variables like ‘character_set_%‘;”回车后显示字符集设置,如下所示:
- mysql> show variables like ‘collation_%‘;
- +----------------------+-------------------+
- | Variable_name | Value |
- +----------------------+-------------------+
- | collation_connection | gb2312_chinese_ci |
- | collation_database | gb2312_chinese_ci |
- | collation_server | gb2312_chinese_ci |
- +----------------------+-------------------+
- 3 rows in set (0.05 sec)
- mysql> show variables like ‘character_set_%‘;
- +--------------------------+----------------------------+
- | Variable_name | Value |
- +--------------------------+----------------------------+
- | character_set_client | gb2312 |
- | character_set_connection | gb2312 |
- | character_set_database | gb2312 |
- | character_set_filesystem | binary |
- | character_set_results | gb2312 |
- | character_set_server | gb2312 |
- | character_set_system | utf8 |
- | character_sets_dir | /usr/share/mysql/charsets/ |
- +--------------------------+----------------------------+
- 8 rows in set (0.00 sec)
- mysql>
根据以上查询结果可知我们设置的MySQL数据库配置信息已经生效,至此完成MySQL的服务器的安装与配置。
3.关于MySQL数据库的一些注意事项
检查你的MySQL配置文件(/etc/my.cnf)中是不是多了一条语句:“skip-grant-tables”,删除(注释)该语句,重新配置MySQL密码,再次重启MySQL服务即可!
备注:若使用skip-grant-tables系统将对任何用户的访问不做任何访问控制,但可以用 mysqladmin flush-privileges或mysqladmin reload来开启访问控制;默认情况是show databases语句对所有用户开放,如果mysql服务器没有开远程帐户,就在/etc/my.cnf里面加上skip-grant-tables。