1. 安裝MySQL數據庫
先從MySQL官網下載MySQL,而後進入所下載的安裝文件所在目錄,運行以下命令進行安裝,其中MySQL-server-community-5.1.56-1.rhel5.i386.rpm爲剛剛下載的MySQL數據庫服務器的rpm包,而後使用/etc/rc.d/init.d/mysqlrestart命令重啓MySQL服務:
- [root@localhost ~]# rpm -ivh MySQL-server-community-5.1.56-1.rhel5.i386.rpm
- [root@localhost ~]# /etc/rc.d/init.d/mysql restart
- Shutting down MySQL..[肯定]
- Starting MySQL..[肯定]
2. 配置MySQL數據庫字符集
備註:配置MySQL數據庫字符集的目的是方便的使用數據庫,無需在每次鏈接的時候都要臨時設置數據庫字符集的,我的不建議採用這種方法,真正的工程項目都應該在鏈接數據庫時臨時設置數據庫字符集,如此才便於系統的移植,並且又不會影響數據庫服務器中的其餘數據庫的使用!html
安裝完成以後,須要配置MySQL的字符集配置,首先須要查找MySQL的配置文件的位置,因爲MySQL的配置文件名是以.cnf結尾的,所以可用以下命令進行查找:mysql
- [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,其命令以下所示:sql
- [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命令模式,以下所示:socket
- [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的服務器的安裝與配置。spa
3.關於MySQL數據庫的一些注意事項
3.1 遠程鏈接mysql速度慢
解決方法:
在MySQL服務器的配置(/etc/my.cnf)中增長一個以下配置後速度飛快。
- [mysqld]
- skip-name-resolve
備註:這樣就能禁用DNS解析,鏈接速度會快不少。不過,這樣的話就不能在MySQL的受權表中使用主機名了而只能用ip格式。
3.2 重啓數據庫後,發現無需密碼(或者任何密碼)便可以鏈接
解決方法:
檢查你的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。