Linux下MySQL數據庫安裝與配置

1. 安裝MySQL數據庫

先從MySQL官網下載MySQL,而後進入所下載的安裝文件所在目錄,運行以下命令進行安裝,其中MySQL-server-community-5.1.56-1.rhel5.i386.rpm爲剛剛下載的MySQL數據庫服務器的rpm包,而後使用/etc/rc.d/init.d/mysqlrestart命令重啓MySQL服務:
 
  1. [root@localhost ~]# rpm -ivh MySQL-server-community-5.1.56-1.rhel5.i386.rpm  
  2. [root@localhost ~]# /etc/rc.d/init.d/mysql restart  
  3. Shutting down MySQL..[肯定]  
  4. Starting MySQL..[肯定]  

2. 配置MySQL數據庫字符集

備註:配置MySQL數據庫字符集的目的是方便的使用數據庫,無需在每次鏈接的時候都要臨時設置數據庫字符集的,我的不建議採用這種方法,真正的工程項目都應該在鏈接數據庫時臨時設置數據庫字符集,如此才便於系統的移植,並且又不會影響數據庫服務器中的其餘數據庫的使用!html

安裝完成以後,須要配置MySQL的字符集配置,首先須要查找MySQL的配置文件的位置,因爲MySQL的配置文件名是以.cnf結尾的,所以可用以下命令進行查找:mysql

 
  1. [root@localhost ~]# find / -iname '*.cnf' -print  
  2. /usr/share/mysql/my-large.cnf  
  3. /usr/share/mysql/my-medium.cnf  
  4. /usr/share/mysql/my-innodb-heavy-4G.cnf  
  5. /usr/share/mysql/my-huge.cnf  
  6. /usr/share/mysql/my-small.cnf  
  7. /usr/share/doc/MySQL-server-community-5.1.56/my-large.cnf  
  8. /usr/share/doc/MySQL-server-community-5.1.56/my-medium.cnf  
  9. /usr/share/doc/MySQL-server-community-5.1.56/my-innodb-heavy-4G.cnf  
  10. /usr/share/doc/MySQL-server-community-5.1.56/my-huge.cnf  
  11. /usr/share/doc/MySQL-server-community-5.1.56/my-small.cnf  
  12. /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

 
  1. [root@localhost ~]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf  
  2. [root@localhost ~]# vi /etc/my.cnf  

而後,使用vi編輯器修改/etc/my.cnf文件,在[client]下添加: 「default-character-set=gb2312」;在[mysqld]下添加:「default-character-set=gb2312」。以下所示:數據庫

 
  1. # The following options will be passed to all MySQL clients  
  2. [client]  
  3. default-character-set=gb2312  
  4. #password       = your_password  
  5. port            = 3306  
  6. socket          = /var/lib/mysql/mysql.sock  
  7.   
  8. # Here follows entries for some specific programs  
  9.   
  10. # The MySQL server  
  11. [mysqld]  
  12. default-character-set=gb2312  
  13. port            = 3306  
  14. socket          = /var/lib/mysql/mysql.sock  
  15. skip-locking  
  16. key_buffer_size = 16M  
  17. max_allowed_packet = 1M  
  18. table_open_cache = 64  
  19. sort_buffer_size = 512K  
  20. net_buffer_length = 8K  

按一下Esc鍵,輸入「:wq」後回車保存配置文件,輸入「/etc/rc.d/init.d/mysqlrestart」重啓MySQL服務,以下所示:服務器

 
  1. [root@localhost ~]# /etc/rc.d/init.d/mysql restart  
  2. Shutting down MySQL..[肯定]  
  3. Starting MySQL..[肯定]  

最後,咱們來驗證MySQL服務器配置是否成功,首先登陸MySQL,輸入「mysql –uroot -p」回車,系統提示輸入密碼,登陸成功後進入MySQL命令模式,以下所示:socket

 
  1. [root@localhost ~]# mysql -uroot -p  
  2. Enter password:   
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.  
  4. Your MySQL connection id is 2  
  5. Server version: 5.1.56-community-log MySQL Community Server (GPL)  
  6. Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.  
  7. Oracle is a registered trademark of Oracle Corporation and/or its  
  8. affiliates. Other names may be trademarks of their respective  
  9. owners.  
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  11. mysql>  

在MySQL命令模式下分別輸入「show variables like'collation_%';」、「show variables like 'character_set_%';」回車後顯示字符集設置,以下所示:編輯器

 
  1. mysql> show variables like 'collation_%';  
  2. +----------------------+-------------------+  
  3. | Variable_name        | Value             |  
  4. +----------------------+-------------------+  
  5. | collation_connection | gb2312_chinese_ci |  
  6. | collation_database   | gb2312_chinese_ci |  
  7. | collation_server     | gb2312_chinese_ci |  
  8. +----------------------+-------------------+  
  9. 3 rows in set (0.05 sec)  
  10. mysql> show variables like 'character_set_%';  
  11. +--------------------------+----------------------------+  
  12. | Variable_name            | Value                      |  
  13. +--------------------------+----------------------------+  
  14. | character_set_client     | gb2312                     |  
  15. | character_set_connection | gb2312                     |  
  16. | character_set_database   | gb2312                     |  
  17. | character_set_filesystem | binary                     |  
  18. | character_set_results    | gb2312                     |  
  19. | character_set_server     | gb2312                     |  
  20. | character_set_system     | utf8                       |  
  21. | character_sets_dir       | /usr/share/mysql/charsets/ |  
  22. +--------------------------+----------------------------+  
  23. 8 rows in set (0.00 sec)  
  24. mysql>  

根據以上查詢結果可知咱們設置的MySQL數據庫配置信息已經生效,至此完成MySQL的服務器的安裝與配置。spa

3.關於MySQL數據庫的一些注意事項

3.1 遠程鏈接mysql速度慢

解決方法:
在MySQL服務器的配置(/etc/my.cnf)中增長一個以下配置後速度飛快。
 
  1. [mysqld]  
  2. 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.net

    原始做者:季亞rest

相關文章
相關標籤/搜索