Centos 6.2下MYSQL主从同步测试
1、IP设定
[root@localhost ~]#vi /etc/sysconfig/network-scripts/ifcfg-eth0
添加IPADDR,NETMASK,GATEWAY并修改ONBOOT为yes
DEVICE="eth0"
HWADDR="00:0C:29:93:35:0D"
NM_CONTROLLED="yes"
IPADDR=192.168.0.198
NETMASK=255.255.255.0
GATEWAY=192.168.0.5
ONBOOT="yes
设置DNS
[root@localhost ~]#vi /etc/resolv.conf 加入nameserver
nameserver 202.96.128.86
2、对于入门的同学可以先将iptables及selinux暂时关闭
[root@localhost ~]#vi /etc/sysconfig/selinux 将SELINUX=enforcing改为SELINUX=disabled
[root@localhost ~]#vi /etc/rc.d/rc.local 添加/etc/init.d/iptables stop让它在开机初始时关闭
MYSQL主从同步
前期已搭载过LAMP环境,所以就直接针对从服务器进行安装测试。
1、下载mysql安装包
2、安装mysql所以需环境
mysql依赖gcc、gcc-c++、ncurses-devel
[root@localhost ~]#yum install gcc gcc-c++ 采用yum安装方便的解决依赖关系
3、安装mysql
[root@localhost ~]#tar vzxf mysql-5.5.21.tar.gz
[root@localhost ~]#cd mysql-5.5.21
[root@localhost ~]#cmake .
[root@localhost ~]#make&&make install
[root@localhost ~]#groupadd -g mysql 创建mysql组
[root@localhost ~]#useradd -r -g mysql mysql 创建mysql用户,-r(创 建的账号为系统账号:该账号的UID会有限制)-g将mysql加入mysql组
[root@localhost ~]#cd /usr/local/mysql 默认mysql安装在此
[root@localhost mysql]#chown -R mysql . 将/usr/local/mysql目录下所 有文件属主改为mysql
[root@localhost mysql]#chgrp -R mysql . 将/usr/local/mysql目录下所 有文件组改为mysql
[root@localhost mysql]#cp support-files/my-medium.cnf /etc/my.cnf
[root@localhost mysql]#scripts/mysql_install_db --user=mysql 初始化 表,以mysql用户
[root@localhost mysql]#chown -R root . 将当前文件夹属主改为root
[root@localhost mysql]#chown -R mysql data 将data文件夹属主改为mysql
[root@localhost mysql]#echo /usr/local/mysql/lib/">>/etc/ld.so.conf更新lib
[root@localhost mysql]#ldconfig 重新加载lib
[root@localhost mysql]#vi /etc/profile 编辑profile文件加入下面内容
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]#source /etc/profile 重新加载
[root@localhost mysql]#cp support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql]#chkconfig --add mysql 添加mysql为服务
[root@localhost mysql]#./bin/mysqladmin -u root password '123456' 为root设置访问数据库的密码
[root@localhost mysql]#mysqld_safe --user=mysql &
至此从服务器安装完成,主MYSQL安装一样。
主从服务器配置文件设置
master:
[root@localhost ~]# vi /etc/my.cnf
server-id = 1 确认此项为1
slave:
[root@localhost ~]# vi /etc/my.cnf
server-id = 2 确认此项为>1,都可以
启动主从MYSQL服务
[root@localhost ~]#service mysql start
授权slave
mysql> grant replication slave on *.* to 'root'@'192.168.0.199' identified by '123456';
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000025 | 511 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记住File、Position的值。
slave服务器操作:
mysql> change master to master_host='192.168.0.199',master_user='root',master_password='123456',master_port=3306,master_log_file='mysql-bin.000025',master_log_pos=511;
mysql> slave start; 启动slave
mysql> show slave status\G; 检查运行状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.199
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000025
Read_Master_Log_Pos: 511
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 657
Relay_Master_Log_File: mysql-bin.000025
Slave_IO_Running: Yes 此两项一定要为Yes,不然就表示未同步成功
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 511
Relay_Log_Space: 817
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)
注: Slave_IO_Running在测试过程中发现此项为No,日志存放在/usr/local/mysql/data/localhost.localdomain.err下,我的错误日志如下:
',replication started in log 'mysql-bin.000025' at position 640
120324 0:09:03 [ERROR] Error reading packet from server: Client requested master to start replication from impossible position; the start event position from 'mysql-bin.000025' at 640, the last event was read from './mysql-bin.000025' at 4, the last byte read was read from './mysql-bin.000025' at 4. ( server_errno=1236)120324 0:09:03 [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log: 'Client requested master to start replication from impossible posit
ion; the start event position from 'mysql-bin.000025' at 640, the last event was read from './mysql-bin.000025' at 4, the last byte read was read from './mysql-bin.000025' at 4.', Error_code: 1236
后来查看master的Position,发现不一致
mysql>slave stop; 停止slave
mysql>reset slave; 重置slave
mysql>change master to master_host='192.168.0.199',master_user='root',master_password='123456',master_port=3306,master_log_file='mysql-bin.000025',master_log_pos=511;
mysql>slave start;
重新配置后解决.
主从同步测试:
master :
mysql> create database test_db;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| discuz |
| mysql |
| performance_schema |
| test |
| test_db |
| ucenter |
+--------------------+
8 rows in set (0.00 sec)
slave:
mysql>show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test_db |
+--------------------+
同步成功完成!