在同一台機器上運行多個 MySQL 服務

來源:互聯網
上載者:User

**********************************************************

第一部分, 在一台伺服器構建多mysql 服務.

**********************************************************

一,緒言

在Mysql中有一mysqld_multi命令,可用於在一台物理伺服器運行多個Mysql服務,今天參考一些文檔,親自測試並通過,真高興,現將操作過程共用給大家!

作業系統:Linux 2.6.13 (Slackware),其它版的Linux應該也差不多.

資料庫:Mysql 4.0.17 來源程式安裝(相信最新的5.1.*也差不多,過些天再試試)

規劃:運行4個mysql服務:

假設伺服器名:db-app (IP為192.168.0.100),

假設伺服器名:db-app2 (IP為192.168.0.101),

下文直到第二部分,均為講述db-app中的mysql,

二,準備

用mysql來源程式安裝,假設在安裝時用的configura選擇項氯?

./configure --prefix=/usr/local/mysql --datadir=/usr/local/mysql/data1 --sysconfdir=/etc

備忘:--prefix將MYSQL安裝到/usr/local/mysql,

--datadir將資料庫產生/usr/local/mysql/data1

sysconfdir是指定mysql使用到的my.cnf設定檔的搜尋路徑為/etc

其他mysql安裝過程略.

根據Mysql管理手冊中提到:每個Mysql的服務都可為獨立的,所以它都調用一個my.cnf中各自不同的啟動選項--就是下文中將提到的GNR值,使用不同的連接埠,產生各自的套接檔案,服務的資料庫都是獨立的(更多可查閱mysql官方網站的英文管理手冊).

mysqld_multi是管理多個mysqld的服務進程,這些服務進程程式不同的unix socket或是監聽於不同的連接埠。他可以啟動、停止和監控當前的服務狀態。

----程式在my.cnf(或是在--config-file自訂的設定檔)中搜尋[mysqld#]段,"#"可以是任意的正整數。這個正整數就是在下面提及的段序列,即GNR。段的序號做為mysqld_multi的參數,來區別不同的段,這樣你就可以控制特定mysqld進程的啟動、停止或得到他的報告資訊。這些組裡的參數就像啟動一個mysqld所需要的組的參數一樣。但是,如果使用多服務,必須為每個服務指定一個unix socket或連接埠(摘自http://mifor.4dian.org中的使用mysqld_multi程式管理多個MySQL服務 )。

從上述文字可看到多Mysql服務中最重要的就是my.cnf設定檔了.

現我貼出我的my.cnf檔案.-----------------------

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

user = mysql (用一個帳號來啟動所有的mysql伺服器,因為是用一相同的帳號。那個麼這帳號必須都是每個mysql服務都要用的帳號,最好是管理帳號,下面的口令與相同)

password = mypaswd

[mysqld1]

port = 3306

socket = /tmp/mysql.sock1

pid-file=/usr/local/mysql/data1/db-app1.pid

log=/usr/local/mysql/data1/db-app.log

datadir = /usr/local/mysql/data

user = mysql

[mysqld2]

port = 3307

socket = /tmp/mysql.sock2

pid-file = /usr/local/mysql/data2/db-app2.pid

datadir = /usr/local/mysql/data2

log=/usr/local/mysql/data2/db-app.log

user = mysql

[mysqld3]

port = 3308

socket = /tmp/mysql.sock3

pid-file = /usr/local/mysql/data3/db-app3.pid3

datadir = /usr/local/mysql/data3

log=/usr/local/mysql/data3/db-app.log

user = mysql

[mysqld4]

port = 3309

socket = /tmp/mysql.sock4

pid-file = /usr/local/mysql/data3/db-app4.pid

datadir = /usr/local/mysql/data4

log=/usr/local/mysql/data4/db-app.log

user = mysql

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

[isamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[myisamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

我的設定檔中有mysqld1,mysqld2,mysqld3,mysqld4。就是說我將啟動4個mysql服務在同一伺服器的不同連接埠——3306-3309,每datadir所指定的資料庫檔案路徑都是不相同的,都有各自不同的記錄檔。其它一些設定可用my.cnf 原來的內容.

ok, 重要的my.cnf編好的,現在為配置中不同mysql 服務建立各自檔案夾和初始資料庫等.

[mysqld1]是一個預設的,在我們安裝mysql時已經有了,所以不用管它.

[mysqld2],只要根據配置就的路徑為它建立一個目錄就可以了.將把該目錄改為mysql管理權

db-app:/ # mkdir /usr/local/mysql/data2

建資料庫,我們可以把預設的mysql資料庫複寫過來,以利用其它的mysql帳號,其它資料庫根據應用再建立.

db-app:/ # cp /usr/local/mysql/data1/mysql /usr/local/mysql/data2 -R

db-app:/ # chmod mysql.mysql /usr/local/mysql/data2 -R

[mysqld3],[mysqld4], 相同.

我們可能看看這些目錄是否都存在.

db-app:/ # ls -l /usr/local/mysql/

drwxr-xr-x 6 mysql mysql 4096 Apr 9 17:54 data4

drwxr-x--- 2 mysql mysql 4096 Apr 9 17:14 data1

drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data2

drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data3

現在可以通過mysqld_multi啟動了。

三,mysqld_multi命令.

使用如下參數來啟動mysqld_multi: (注:該命令在mysql的bin目錄中,根據上面所提到./configure --prefix=/usr/local/mysql ,所以該檔案應該在 /usr/local/mysq/bin, 這得根據你安裝時所指定的路徑 )

db-app:/ # mysqld_multi [options] {start|stop|report} [GNR[,GNR]...]

start,stop和report是指你想到執行的操作。你可以在單獨的服務或是多服務上指定一個操作,區別於選項後面的GNR列表。如果沒有指定GNR列表,那麼mysqld_multi將在所有的服務中根據選項檔案進行操作。

每一個GNR的值是組的序號或是一個組的序號範圍。此項的值必須是組名字最後的數字,比如說如果組名為mysqld17,那麼此項的值則為17.如果指定一個範圍,使用"-"(破折號)來串連二個數字。如GNR的值為10-13,則指組mysqld10到組mysqld13。多個組或是組範圍可以在命令列中指定,使用","(逗號)隔開。不能有空白的字元(如空格或tab),在空白字元後面的參數將會被忽略。 (注:GNR值就是我們定義my.cnf中mysqld#中的值,我這裡只有1-4).

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1 只啟動 第一個mysql服務,相關檔案由my.cnf中mysql1設定.

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf stop 1 啟止 第一個mysql服務

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4, 啟動 第1至4mysql服務,其實就是我這裡的全部.

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf report 1-4

查看啟動:

db-app:/ # ps aux

root 10467 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3306 --socket=/tmp/mysql.sock1

root 10475 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3307 --socket=/tmp/mysql.sock2

root 10482 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3308 --socket=/tmp/mysql.sock3

root 10487 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3309 --socket=/tmp/mysql.sock4

.............................................

四,用戶端訪問

任何用戶端訪問都需要指定訪問連接埠.方才能進入指定資料庫服務.否則將使用到Mysql預設的連接埠(3306)所服務的MYSQL。

**********************************************************

第二部分, 在一台伺服器構建多mysql主 服務.

**********************************************************

提示:建立一個帳號,專門用於下面主從複製,本例用的的帳號repl, 口令為:'1234567890', 可讓其擁有select_priv, reload_priv,process_priv,grant_priv,super_priv,repl_slave_priv,repl_client_priv許可權,並能通過任何客戶進行訪問,即訪問客戶為'%'。

主要是修改my.cnf中的內容,讓每個mysql產生自己的bin-log檔案及各自的運行環境,現貼上我的my.cnf全部內容,相關參數與請參考mysql 官文手冊.

#[client]

#password = your_password

#port = 3306

#socket = /tmp/mysql.sock

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

user = mysql

password = mypasswd

[mysqld1]

port = 3306

socket = /tmp/mysql.sock1

skip-locking

pid-file=/usr/local/mysql/data/net-app1a.pid

datadir = /usr/local/mysql/data

log=/usr/local/mysql/data/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data/slowquery.log

long_query_time = 2

key_buffer = 256M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

myisam_sort_buffer_size = 64M

thread_cache = 32

query_cache_size = 32M

thread_concurrency = 2

max_connections=500

log-bin

log-bin=/usr/local/mysql/data/app-net1_1-bin

server-id = 1

[mysqld2]

port = 3307

socket = /tmp/mysql.sock2

pid-file = /usr/local/mysql/data2/net-app1b.pid

datadir = /usr/local/mysql/data2

log=/usr/local/mysql/data2/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data2/slowquery.log

long_query_time = 10

key_buffer = 128M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 1M

read_buffer_size = 1M

myisam_sort_buffer_size = 32M

thread_cache = 32

query_cache_size = 16M

thread_concurrency = 2

max_connections=300

log-bin

log-bin=/usr/local/mysql/data2/app-net1_2-bin

server-id = 1

[mysqld3]

port = 3308

socket = /tmp/mysql.sock3

skip-locking

pid-file = /usr/local/mysql/data3/net-app1c.pid

datadir = /usr/local/mysql/data3

log=/usr/local/mysql/data3/net-app1.log

user = mysql

log-bin

log-bin=/usr/local/mysql/data3/app-net1_3-bin

server-id = 1

[mysqld4]

port = 3309

socket = /tmp/mysql.sock4

skip-locking

pid-file = /usr/local/mysql/data1/app-net1d.pid

datadir = /usr/local/mysql/data1

log=/usr/local/mysql/data1/net-app1.log

user = mysql

log-bin

log-bin=/usr/local/mysql/data1/app-net1_4-bin

server-id = 1

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[isamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[myisamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

**********************

啟動多mysql服務都是相同的,

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4

**********************************************************

第三部分, 在一台伺服器構建多mysql 從服務.

**********************************************************

構建從伺服器的預選準備: 建議用mysqld_multi 把主伺服器的mysql全部停掉.刪除資料目錄中的所有除資料庫目錄以外的任何檔案(此文中的資料目錄庫有4個,datadir = /usr/local/mysql/中的 data1 -- data1).建主從都用相同的資料目錄路徑.

用Tar 命令把每資料庫封裝起來,並通過sftp命令put/get到從伺服器(db-app1 192.168.0.101).

下列操作供參考:

在db-app主機上的操作

db-app:/ # tar -cf data1.tar /usr/local/mysql/data1

db-app:/ # tar -cf data2.tar /usr/local/mysql/data2

db-app:/ # tar -cf data3.tar /usr/local/mysql/data3

db-app:/ # tar -cf data4.tar /usr/local/mysql/data4

在db-app1主機上的操作

db-app1:/ # tar xvf data1.tar

db-app1:/ # tar xvf data2.tar

db-app1:/ # tar xvf data3.tar

db-app1:/ # tar xvf data4.tar

同時,請確認系統帳號mysql是否對主/從伺服器的中的mysql資料目錄都有操作許可權,如果無法確認,你直接更修改這些目錄的所有權即可。

在db-app主機上的操作

db-app:/ # chown mysql.mysql /usr/local/mysql/data1 -R

db-app:/ # chown mysql.mysql /usr/local/mysql/data2 -R

db-app:/ # chown mysql.mysql /usr/local/mysql/data3 -R

db-app:/ # chown mysql.mysql /usr/local/mysql/data4 -R

在db-app1主機上的操作

db-app1:/ # chown mysql.mysql /usr/local/mysql/data1 -R

db-app2:/ # chown mysql.mysql /usr/local/mysql/data2 -R

db-app3:/ # chown mysql.mysql /usr/local/mysql/data3 -R

db-app4:/ # chown mysql.mysql /usr/local/mysql/data4 -R

下面就是從伺服器上/etc/my.cnf的全部內容.

提示:下面的my.cnf中將會提到一個帳號:repl, 口令為:'1234567890', 這個帳號就是上面專門建立的。

其實都一樣,主要是修改my.cnf中的內容,讓每個從mysql通過主mysql的不同的連接埠,去擷取各自bin-log來更新自生的資料庫內容.現貼上我的my.cnf全部內容(從伺服器),相關參數與請參考mysql 官文手冊.

#[client]

#password = your_password

#port = 3306

#socket = /tmp/mysql.sock

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin = /usr/local/mysql/bin/mysqladmin

user = mysql

password = netmoniit

[mysqld1]

port = 3306

socket = /tmp/mysql.sock1

skip-locking

pid-file=/usr/local/mysql/data/net-app1a.pid

datadir = /usr/local/mysql/data

log=/usr/local/mysql/data/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data/slowquery.log

long_query_time = 2

key_buffer = 256M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 2M

read_buffer_size = 2M

myisam_sort_buffer_size = 64M

thread_cache = 32

query_cache_size = 32M

thread_concurrency = 2

max_connections=500

server-id = 2

master-host = 192.168.0.100

master-user = 'repl'

master-password = '1234567890'

master-port = 3309

report-host = net-app1

master-connect-retry = 30

log-bin

log-slave-updates

[mysqld2]

port = 3307

socket = /tmp/mysql.sock2

pid-file = /usr/local/mysql/data2/net-app1b.pid

datadir = /usr/local/mysql/data2

log=/usr/local/mysql/data2/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data2/slowquery.log

long_query_time = 10

key_buffer = 128M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 1M

read_buffer_size = 1M

myisam_sort_buffer_size = 32M

thread_cache = 32

query_cache_size = 16M

thread_concurrency = 2

max_connections=300

server-id = 2

master-host = 192.168.0.100

master-user = 'repl'

master-password = '1234567890'

master-port = 3309

report-host = net-app1

master-connect-retry = 30

log-bin

log-slave-updates

[mysqld3]

port = 3308

socket = /tmp/mysql.sock3

pid-file = /usr/local/mysql/data3/net-app1c.pid

datadir = /usr/local/mysql/data3

log=/usr/local/mysql/data3/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data3/slowquery.log

long_query_time = 10

key_buffer = 128M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 1M

read_buffer_size = 1M

myisam_sort_buffer_size = 32M

thread_cache = 32

query_cache_size = 16M

thread_concurrency = 2

max_connections=300

server-id = 2

master-host = 192.168.0.100

master-user = 'repl'

master-password = '1234567890'

master-port = 3309

report-host = net-app1

master-connect-retry = 30

log-bin

log-slave-updates

[mysqld3]

port = 3308

socket = /tmp/mysql.sock4

pid-file = /usr/local/mysql/data4/net-app1d.pid

datadir = /usr/local/mysql/data4

log=/usr/local/mysql/data4/net-app1.log

user = mysql

log-slow-queries=/usr/local/mysql/data4/slowquery.log

long_query_time = 10

key_buffer = 128M

max_allowed_packet = 1M

table_cache = 512

sort_buffer_size = 1M

read_buffer_size = 1M

myisam_sort_buffer_size = 32M

thread_cache = 32

query_cache_size = 16M

thread_concurrency = 2

max_connections=300

server-id = 2

master-host = 192.168.0.100

master-user = 'repl'

master-password = '1234567890'

master-port = 3309

report-host = net-app1

master-connect-retry = 30

log-bin

log-slave-updates

[mysqldump]

quick

max_allowed_packet = 16M

[mysql]

no-auto-rehash

# Remove the next comment character if you are not familiar with SQL

#safe-updates

[isamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[myisamchk]

key_buffer = 128M

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

****************************************

在功告成,現在分別啟動兩台主機上的多mysql服務,這樣,每個主服務的每個mysql有變化,都會自動複製/更新到從伺服器對應的資料庫中。

db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4

db-app1:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4

********************

五,未來測試,

接下來我想做一個多主一從的Mysql伺服器複製解決方案! 那位可以給點意見啊!結構如下.

就是有主伺服器Server A , Server B和從伺服器 Server C, A 和 B運行著不同的資料庫應用, 假設資料庫名都不同。Server C(假設這三台PC上都只運行了一個mysql服務),包括了A了B伺服器的所有Mysql 使用者及相同的存取權限, 並整合在一個Mysql服務中. C通過主/從方式複製A和B的資料庫。

就是差不就是把兩個主伺服器的mysql合并到一個從伺服器中。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.