天天看點

linux 如何裝mysql資料庫_Linux下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資料庫的一些注意事項