1. 程式人生 > >從零開始搭建完整分散式系統

從零開始搭建完整分散式系統

1.       執行平臺:CentOS 6.3 x86_64,基本等同於RHEL 6.3

2.       安裝方法:

安裝MySQL主要有兩種方法:一種是通過原始碼自行編譯安裝,這種適合高階使用者定製mysql的特性,這裡不做說明;另一種是通過編譯過的二進位制檔案進行安裝。二進位制檔案安裝的方法又分為兩種:一種是不針對特定平臺的通用安裝方法,使用的二進位制檔案是字尾為.tar.gz的壓縮檔案;第二種是使用RPM或其他包進行安裝,這種安裝程序會自動完成系統的相關配置,所以比較方便。

3.       下載安裝包:

a.  官方下載地址:

或映象檔案下載:

2.  下載檔案(根據作業系統選擇相應的釋出版本):

a.  通用安裝方法

mysql-5.5.29-linux2.6-x86_64.tar.gz

b.       RPM安裝方法:

MySQL-server-5.5.29-2.el6.x86_64.rpm

MySQL-client-5.5.29-2.el6.x86_64.rpm

4.       通用安裝步驟

a.       檢查是否已安裝,grep的-i選項表示匹配時忽略大小寫

[[email protected] JavaEE]#rpm -qa|grep -i mysql

mysql-libs-5.1.61-4.el6.x86_64

*可見已經安裝了庫檔案,應該先解除安裝,不然會出現覆蓋錯誤。注意卸:載時使用了--nodeps選項,忽略了依賴關係:

[[email protected] JavaEE]#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps

b.     新增mysql組和mysql使用者,用於設定mysql安裝目錄檔案所有者和所屬組。

[[email protected] JavaEE]#groupadd mysql

[[email protected] JavaEE]#useradd -r -g mysql mysql

*useradd -r引數表示mysql使用者是系統使用者,不可用於登入系統。

c.  將二進位制檔案解壓到指定的安裝目錄,我們這裡指定為/usr/local

[[email protected] ~]# cd/usr/local/

[[email protected] local]#tar zxvf /path/to/mysql-5.5.29-linux2.6-x86_64.tar.gz

*加壓後在/usr/local/生成了解壓後的資料夾mysql-5.5.29-linux2.6-x86_64,這名字太長,我們為它建立一個符號連結mysql,方便輸入。

[[email protected] local]#ln -s mysql-5.5.29-linux2.6-x86_64 mysql

d.     /usr/local/mysql/下的目錄結構

Directory

Contents of Directory

bin

Client programs and the mysqld server

data

Log files, databases

docs

Manual in Info format

man

Unix manual pages

include

Include (header) files

lib

Libraries

scripts

mysql_install_db

share

Miscellaneous support files, including error messages, sample configuration files, SQL for database installation

sql-bench

Benchmarks

e.     進入mysql資料夾,也就是mysql所在的目錄,並更改所屬的組和使用者。

[[email protected] local]#cd mysql

[[email protected] mysql]#chown -R mysql .

[[email protected] mysql]#chgrp -R mysql .

f.       執行mysql_install_db指令碼,對mysql中的data目錄進行初始化並建立一些系統表格。注意mysql服務程序mysqld執行時會訪問data目錄,所以必須由啟動mysqld程序的使用者(就是我們之前設定的mysql使用者)執行這個指令碼,或者用root執行,但是加上引數--user=mysql。

[[email protected] mysql]scripts/mysql_install_db --user=mysql

*如果mysql的安裝目錄(解壓目錄)不是/usr/local/mysql,那麼還必須指定目錄引數,如

[[email protected] mysql]scripts/mysql_install_db --user=mysql \

         --basedir=/opt/mysql/mysql \

         --datadir=/opt/mysql/mysql/data

*將mysql/目錄下除了data/目錄的所有檔案,改回root使用者所有,mysql使用者只需作為mysql/data/目錄下所有檔案的所有者。

[[email protected] mysql]chown -R root .

[[email protected] mysql]chown -R mysql data

g.     複製配置檔案

[[email protected] mysql] cp support-files/my-medium.cnf /etc/my.cnf

h.  將mysqld服務加入開機自啟動項。

*首先需要將scripts/mysql.server服務指令碼複製到/etc/init.d/,並重命名為mysqld。

[[email protected]]  cp support-files/mysql.server /etc/init.d/mysqld

*通過chkconfig命令將mysqld服務加入到自啟動服務項中。

[[email protected] mysql]#chkconfig --add mysqld

*注意服務名稱mysqld就是我們將mysql.server複製到/etc/init.d/時重新命名的名稱。

*檢視是否新增成功

[[email protected] mysql]#chkconfig --list mysqld

mysqld   0:off 1:off        2:on        3:on        4:on        5:on        6:off

i.  重啟系統,mysqld就會自動啟動了。

*檢查是否啟動

[[email protected] mysql]#netstat -anp|grep mysqld

tcp        0     0 0.0.0.0:3306               0.0.0.0:*                   LISTEN      2365/mysqld        

unix  2     [ ACC ]     STREAM     LISTENING     14396 2365/mysqld        /tmp/mysql.sock

*如果不想重新啟動,那可以直接手動啟動。

[[email protected] mysql]#service mysqld start

Starting MySQL.. SUCCESS!

j.       執行客戶端程式mysql,在mysql/bin目錄中,測試能否連線到mysqld。

[[email protected] mysql]#/usr/local/mysql/bin/mysql

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 2

Server version:5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

oracle is a registeredtrademark of Oracle Corporation and/or its affiliates. Other names may betrademarks of their respective owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql> quit

Bye

*此時會出現mysql>命令提示符,可以輸入sql語句,輸入quit或exit退出。為了避免每次都輸入mysql的全路徑/usr/local/mysql/bin/mysql,可將其加入環境變數中,在/etc/profile最後加入兩行命令:

MYSQL_HOME=/usr/local/mysql

export PATH=$PATH:$MYSQL_HOME/bin

這樣就可以在shell中直接輸入mysql命令來啟動客戶端程式了

[[email protected] mysql]#mysql

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 3

Server version:5.5.29-log MySQL Community Server (GPL)

Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

Oracle is a registeredtrademark of Oracle Corporation and/or its

affiliates. Other namesmay be trademarks of their respective

owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql>

5.       RPM安裝步驟

a.       檢查是否已安裝,grep的-i選項表示匹配時忽略大小寫

[[email protected] JavaEE]#rpm -qa|grep -i mysql

mysql-libs-5.1.61-4.el6.x86_64

可見已經安裝了庫檔案,應該先解除安裝,不然會出現覆蓋錯誤。注意解除安裝時使用了--nodeps選項,忽略了依賴關係:

[[email protected] JavaEE]#rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps

2.     安裝MySQL的伺服器端軟體,注意切換到root使用者:

[[email protected] JavaEE]#rpm -ivh MySQL-server-5.5.29-2.el6.x86_64.rpm

安裝完成後,安裝程序會在Linux中新增一個mysql組,以及屬於mysql組的使用者mysql。可通過id命令檢視:

[[email protected] JavaEE]#id mysql

uid=496(mysql)gid=493(mysql) groups=493(mysql)

MySQL伺服器安裝之後雖然配置了相關檔案,但並沒有自動啟動mysqld服務,需自行啟動:

[[email protected] JavaEE]#service mysql start

Starting MySQL.. SUCCESS!

可通過檢查埠是否開啟來檢視MySQL是否正常啟動:

[[email protected] JavaEE]#netstat -anp|grep 3306

tcp        0     0 0.0.0.0:3306               0.0.0.0:*                   LISTEN      34693/mysqld

c.  安裝MySQL的客戶端軟體:

[[email protected] JavaEE]#rpm -ivh MySQL-client-5.5.29-2.el6.x86_64.rpm

如果安裝成功應該可以執行mysql命令,注意必須是mysqld服務以及開啟:

[[email protected] JavaEE]#mysql

Welcome to the MySQLmonitor.  Commands end with ; or \g.

Your MySQL connection idis 1

Server version: 5.5.29MySQL Community Server (GPL)

Copyright (c) 2000, 2012,Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademarkof Oracle Corporation and/or its affiliates. Other names may be trademarks oftheir respective owners.

Type 'help;' or '\h' forhelp. Type '\c' to clear the current input statement.

mysql>

d.  RPM安裝方式檔案分佈

Directory

Contents of Directory

/usr/bin

Client programs and scripts

/usr/sbin

The mysqld server

/var/lib/mysql

Log files, databases

/usr/share/info

Manual in Info format

/usr/share/man

Unix manual pages

/usr/include/mysql

Include (header) files

/usr/lib/mysql

Libraries

/usr/share/mysql

Miscellaneous support files, including error messages, character set files, sample configuration files, SQL for database installation

/usr/share/sql-bench

Benchmarks