...
一)环境拓扑今天讨论的是mysql集群这一块,资源如下所示:二)Mysql安装配置这里不一一赘述,可以选择源代码编译安装,也可以参考我之前的一篇博文“懒人“速成
一)环境拓扑
二)Mysql安装配置
这里不一一赘述,可以选择源代码编译安装,也可以参考我之前的一篇博文“懒人“速成——linux LAMP环境。
三)节点配置
主节点:
1.首先在数据库中建立2个数据库和表:
#service mysqld start
#mysql
mysql>create database www;
mysql>use www;
mysql>create table www(id int);
mysql>insert into www values(1);
mysql> select * from www;
查看数据: ;
同理创建blog数据库和表:
mysql>create database blog;
mysql>use blog;
mysql>create table blog(id int);
mysql>insert into blog values(1);
mysql> select * from blog;
2.修改My.cnf配置文件
vi etc/my.cnf[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
//日志为2进制,不需要更改
server-id =1
//为1就是Master,不需要更改
binlog-do-db=blog
//需要同步的库
binlog-do-db=www
//需要同步的库
binlog-ignore-db=mysql,test,information_schema //不需要记录日志的数据库名,多个数据库中间用逗号(,)隔开
innodb_data_home_dir = /usr/local/mysql/data/
//innodb的表空间位置
innodb_data_file_path = ibdata1:50M:autoextend
//表空间的名字,开始50M
innodb_log_group_home_dir = /usr/local/mysql/data/
innodb_buffer_pool_size = 256M
//为系统内存的50-80%
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
3.重启数据库
[root@Test01 /]# service mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
4.创建有权限的账号,让Slave数据库访问主数据库
[root@Test01 /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.69-log Source distribution
Copyright (c) 2000, 2013, 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> grant replication slave on *.* to repl@"192.168.1.26" identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql>flush privileges;
;
5.备份Master数据库
mysql> flush tables with read lock; //不要退出这个终端,否则这个锁就不生效了。
同时取得快照,并记录日志和偏移量:
mysql> show master status;
+------------------+----------+--------------+-------------------------------+
| File
| Position | Binlog_Do_DB | Binlog_Ignore_DB
|
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 |
196 | blog,www
| mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)
6.开启另一个终端,对主数据库做快照
[root@Test01 mysql]#cd /var/lib/mysql/
[root@Test01 mysql]#tar -zcvf backup.tar.gz www blog
7.库解锁
mysql> unlock tables;
从节点:
1.安装mysql,方法同上,不在赘述。
2.修改My.cnf配置文件
[root@Test02 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id = 2
#master-host
= 192.168.1.10
#master-user
= root
#master-password = 584911644
#master-port
= 3306
#master-log-pos = 196
#master-log-file = mysql-bin.000001
#master-connect-retry=60
replicate-do-db=www
//告诉slave只做www数据库的更新
replicate-do-db=blog
//告诉slave只做blog数据库的更新
log-slave-updates
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
"/etc/my.cnf" 23L, 551C
3.把之前主数据库压缩的数据拷贝到对应位置
我这里用的是SecureFX,当然,方法很多,免备案空间,也不细说了,可谓是条条大路通罗马。
4.启动数据库
[root@Test02 mysql]# service mysqld start
Starting mysqld: [ OK ]
5.配置,启动Slave