分身术,一台机器运行多个Mysql 服务 (4)

发表于:2007-05-26来源:作者:点击数: 标签:
********************************************************** 第三部分, 在一台 服务器 构建多 mysql 从服务。 ********************************************************** 构建从服务器的预选准备: 建议用mysqld_multi 把主服务器的mysql全部停掉.删除数

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

第三部分, 在一台服务器构建多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

原文转自:http://www.ltesting.net