今天说的实验是MySQL主从双向复制,实验环境为Master(Red Hat Enterprise Linux Server release 5.5 IP: 192.168.0.225)Slave(CentOS release 6.2 (Final) IP:192.168.0.214)
废话不多说,直接写如何实验过程。
- # cd /usr/local/src/mysql
- # tar xzvf mysql-5.1.45.tar.gz
- # groupadd mysql
- # useradd -g mysql mysql
- # cd mysql-5.1.45
- #./configure --prefix=/usr/local/mysql --enable-local-infile --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --enable-assembler --with-unix-socket-path=/usr/local/mysql/tmp/mysql.sock --with-plugins=innobase
- #make && make install
配置mysql
- # cp support-files/my-medium.cnf /etc/my.cnf
- # 修改 /etc.my.cnf 将skip-locking替换成skip-external-locking
- # cd /usr/local/mysql
- # bin/mysql_install_db --user=mysql
- # mkdir tmp
- # chown -R root .
- # chown -R mysql var
- # chown -R mysql tmp
- # chgrp -R mysql .
- # bin/mysqld_safe --user=mysql & //以mysql用户启动数据库。
同样步骤在slave机器上安装完毕mysql
———————————————————————————————————————————————————————————————————————————————————
master my.cnf配置文件
- [root@defcon ~]# egrep -v '^$|^#' /etc/my.cnf
- [client]
- port = 3306
- socket = /usr/local/mysql/tmp/mysql.sock
- [mysqld]
- port = 3306
- socket = /usr/local/mysql/tmp/mysql.sock
- skip-external-locking
- key_buffer_size = 16M
- max_allowed_packet = 1M
- table_open_cache = 64
- sort_buffer_size = 512K
- net_buffer_length = 8K
- read_buffer_size = 256K
- read_rnd_buffer_size = 512K
- myisam_sort_buffer_size = 8M
- server-id= 1
- master-host=192.168.0.214
- master-user=shanker
- master-password=1234
- master-port=3306
- master-connect-retry=60
- replicate-do-db=testdba
- log-bin
- binlog-do-db=testdba
- read-only=0
- binlog-ignore-db=mysql
- log-bin=mysql-bin
- binlog_format=mixed
- [mysqldump]
- quick
- max_allowed_packet = 16M
- [mysql]
- no-auto-rehash
- [myisamchk]
- key_buffer_size = 20M
- sort_buffer_size = 20M
- read_buffer = 2M
- write_buffer = 2M
- [mysqlhotcopy]
- interactive-timeout
slave的my.cnf配置内容
- [root@shanker ~]# egrep -v '^$|^#' /etc/my.cnf
- [client]
- port = 3306
- socket = /usr/local/mysql/tmp/mysql.sock
- [mysqld]
- port = 3306
- socket = /usr/local/mysql/tmp/mysql.sock
- skip-external-locking
- key_buffer_size = 16M
- max_allowed_packet = 1M
- table_open_cache = 64
- sort_buffer_size = 512K
- net_buffer_length = 8K
- read_buffer_size = 256K
- read_rnd_buffer_size = 512K
- myisam_sort_buffer_size = 8M
- server-id=2
- master-host=192.168.0.225
- master-user=shanker
- master-password=1234
- master-port=3306
- master-connect-retry=60
- replicate-do-db=testdba
- log-bin=/var/mysql/mysqllog
- log-bin=mysql-bin
- binlog_format=mixed
- [mysqldump]
- quick
- max_allowed_packet = 16M
- [mysql]
- no-auto-rehash
- [myisamchk]
- key_buffer_size = 20M
- sort_buffer_size = 20M
- read_buffer = 2M
- write_buffer = 2M
- [mysqlhotcopy]
- interactive-timeout
- server-id=1
- log-bin=/var/mysql/mysqllog
- binlog-do-db=testdb
- read-only=0
- binlog-ignore-db=mysql
配置文件修改完毕保存该配置并且删掉 mysql 的 var 目录下除数据库以外的文件。
主要是 *.00000* 类似的文件以及 master.info ,为避免出错,一定要先删除。
为了方便我写了个脚本,用来删除那些文件
- [root@shanker ~]# cat clearupvar
- cd /usr/local/mysql/var
- rm i* mysql-b* shanker* *.info
shanker是主机的名字
主机上:
- # /usr/local/mysql/bin/mysql -uroot -p
- mysql>create database testdb;
- mysql>use testdb;
- mysql>CREATE TABLE students(
- sn INT,
- name VARCHAR(20)
- )
- mysql> insert into students values(1,'shanker');
从机上:
- /usr/local/mysql/bin/mysql -uroot -p
- mysql>create database testdb;
- mysql>CREATE TABLE students(
- sn INT,
- name VARCHAR(20)
- )
- mysql> insert into students values(2,'defcon');
创建用于同步的用户并赋予其操作权限(主机从机都要执行)
- # /usr/local/mysql/bin/mysql -uroot
- mysql>GRANT REPLICATION SLAVE ON *.* TO 'shanker'@'%' IDENTIFIED BY '1234';
- mysql>flush privileges
主机 master :
mysql > show master status;
记住字段 file 和 position 的值,在 slave 状态中需要检验的。
从机 slave :
mysql > show slave status\G;
特别提示:检查参数 Slave_IO_State 、 Slave_IO_Running 、 Slave_SQL_Running
Slave_IO_State 的状态值是由 Slave_IO_Running 的值决定的,如果
Slave_IO_Running 和 Slave_SQL_Running 的值都为 Yes ,并且
Slave_IO_State 的值为 :Waiting for master to send event ,那么说
明配置是没问题的可以进行下一步同步尝试了。
看截图
done successfully ~~