標籤:
1. 安裝MySQL資料庫先從MySQL官網下載MySQL,然後進入所下載的安裝檔案所在目錄,運行如下命令進行安裝,其中MySQL-server-community-5.1.56-1.rhel5.i386.rpm為剛剛下載的MySQL資料庫伺服器的rpm包,然後使用/etc/rc.d/init.d/mysqlrestart命令重啟MySQL服務:
- [[email protected] ~]# rpm -ivh MySQL-server-community-5.1.56-1.rhel5.i386.rpm
- [[email protected] ~]# /etc/rc.d/init.d/mysql restart
- Shutting down MySQL..[確定]
- Starting MySQL..[確定]
2. 配置MySQL資料庫字元集
備忘:配置MySQL資料庫字元集的目的是方便的使用資料庫,無需在每次串連的時候都要臨時設定資料庫字元集的,個人不建議採用這種方法,真正的工程項目都應該在串連資料庫時臨時設定資料庫字元集,如此才便於系統的移植,而且又不會影響資料庫伺服器中的其他資料庫的使用!
安裝完成之後,需要配置MySQL的字元集配置,首先需要尋找MySQL的設定檔的位置,由於MySQL的設定檔名是以.cnf結尾的,因此可用如下命令進行尋找:
- [[email protected] ~]# 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,其命令如下所示:
- [[email protected] ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
- [[email protected] ~]# 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服務,如下所示:
- [[email protected] ~]# /etc/rc.d/init.d/mysql restart
- Shutting down MySQL..[確定]
- Starting MySQL..[確定]
最後,我們來驗證MySQL伺服器配置是否成功,首先登入MySQL,輸入“mysql –uroot -p”斷行符號,系統提示輸入密碼,登入成功後進入MySQL命令模式,如下所示:
- [[email protected] ~]# 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資料庫的一些注意事項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。
部落格地址:http://blog.csdn.net/shuxiao9058
原始作者:季亞
Linux下MySQL資料庫安裝與配置