一、环境简述
需求描述
操作系统:CentOS 6.5_X64 关闭防火墙和selinux
数据库:MySQL 5.6
数据库分配:
function | ip | hostname | server id |
master 1 | 192.168.137.75 | db1 | master-1 |
master 2 | 192.168.137.33 | db2 | master-2 |
二、安装Mysql
1. 查看是否安装旧版本
使用下面的命令检查是否安装有MySQL Server
#rpm -qa | grep mysql |
2. 卸载MySQL Server旧版本
#rpm -e mysql //普通删除模式 #rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除 |
3. 安装库文件
安装编译代码需要的包
#yum -y install make cmake gcc-c++ bison ncurses-devel mysql |
5. 下载编译安装Mysql 5.6版本
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.14.tar.gz
tar xvf mysql-5.6.14.tar.gz cd mysql-5.6.14 cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DSYSCONFDIR=/etc \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci
make && make install |
6. 配置MySQL
groupadd mysql useradd -g mysql -s /sbin/nologin mysql |
修改权限
chown -R mysql.mysql /usr/local/mysql |
7. 初始化mysql数据库
进入安装路径,执行初始化配置脚本,创建系统自带的数据库和表
# cd /usr/local/mysql scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql |
8. 修改my.cnf配置文件
#vi /etc/my.cnf [mysqld] # These are commonly set, remove the # and set as required. basedir=/usr/local/mysql //安装时定义的安装目录 datadir=/usr/local/mysql/data //安装时定义的数据存放目录 port=3306 //安装时定义的监听端口 #server_id = ..... socket=/var/lib/mysql/mysql.sock //注意此目录权限问题 |
chmod a+rwx /var/lib/mysql/mysql.sock 这个先不用执行、看看mysql是否可以成功启动
9. 添加mysql自启动
添加服务,拷贝服务脚本到init.d目录,并设置开机启动
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig mysqld on service mysqld start |
10. 配置环境变量
MySQL启动成功后,root默认没有密码,我们需要设置root密码。
设置之前,我们需要先设置PATH,使之直接调用mysql
修改/etc/profile文件,在文件末尾添加
#vi /etc/profile PATH=/usr/local/mysql/bin:$PATH export PATH |
关闭文件,运行下面的命令,让配置立即生效
#source /etc/profile |
现在,我们可以在终端内直接输入mysql进入,mysql的环境了
执行下面的命令修改root密码
#mysql -uroot mysql> SET PASSWORD = password(‘123456‘); mysql>flush privileges; #刷新系统授权表 |
sql_mode=NO_ENGINE_SUBSTITUTION
到此,mysql安装完成!
三、开始做同步
mysql -u root -p #进入MySQL控制台
create database xmlgrg; #建立数据库xmlgrg
use xmlgrg #进入数据库
CREATE TABLE test22 ( id int not null primary key,name char(20) ); #创建test22表
#查看xmlgrg表结构
mysql> show tables;
+----------------------+
| Tables_in_xmlgrg |
+----------------------+
| test22 |
+----------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
1、导出db1数据库xmlgrg
备注:在导出之前可以先进入MySQL控制台执行下面命令
flush tables with read lock; #数据库只读锁定命令,防止导出数据库的时候有数据写入
unlock tables; #解除锁定 导出之后在mysql控制台执行
mysqldump -u root -p --default-character-set=utf8 --opt -Q -R --skip-lock-tables xmlgrg> /home/xmlgrg.sql #在MySQL主服务器进行操作,导出数据库xmlgrg到/home/xmlgrg.sql
scp /home/xmlgrg.sql root@192.168.137.33:/home #把home目录下的xmlgrg.sql 数据库文件上传到db2服务器的home目录下面
2、导入数据库到db2服务器
mysql -u root -p #进入db2服务器MySQL控制台
create database xmlgrg; #创建数据库
use xmlgrg #进入数据库
source /home/xmlgrg.sql #导入备份文件到数据库
1、db1和db2做主主同步
db1:
修改mysql配置文件
cp /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
[mysqld]
#mysql同步#
server-id=1 #服务器标示两边都要不一样#
log-bin=mysql-bin #启用记录binlog
binlog-do-db=xmlgrg #记录binlog日志的数据库#
##binlog-ignore-db=mysql #不记录binlog日志的数据库#
replicate-do-db=xmlgrg #要同步的数据库#
##replicate-ignore-db=mysql #不同步的数据库#
log-slave-updates #启用更新binlog,处于master角色的时候必须。
slave-skip-errors=all #跳过不是很重要的错误信息#
sync_binlog=1 #每条自动更新,安全性高,默认是0
auto_increment_increment=2 #每次步长增长值# (红色字体的只在主主的情况下用,几个主数字就写几,避免自增冲突)
auto_increment_offset=1 #起始步长值mei都要不一样#
【做同步的时候如果不选择要同步的数据库默认全部同步】
service mysqld restart #重启服务
登录mysql,创建同步用户并授权,配置master信息,启用slave:
#查看server-id的值是否为1
mysql> show variables like ‘server_id‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
#查看主服务器,出现以下类似信息
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 120 | test | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
注意:这里记住File的值:mysql-bin.000001和Position的值:120,后面会用到。
mysql> grant replication slave on *.* to ‘tongbu‘@‘192.168.137.%‘ identified by ‘tongbu‘;
mysql> flush privileges;
mysql> change master to master_host="192.168.137.33", master_port=3306, master_user="tongbu", master_password="tongbu", master_log_file="mysql-bin.000001", master_log_pos=120, master_connect_retry=10; #执行同步语句
mysql> start slave; #启动slave
mysql> show slave status\G #查看slave 状态
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.137.33
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 421
Relay_Log_File: zabbix-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: xmlgrg
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: 421
Relay_Log_Space: 120
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1130
Last_IO_Error: error connecting to master ‘tongbu@192.168.137.33:3306‘ - retry-time: 10 retries: 1
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 160618 13:53:26
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
db2:
修改mysql配置文件
cp /etc/my.cnf /etc/my.cnf.bak
vim /etc/my.cnf
[mysqld]
#mysql同步#
server-id=2 #服务器标示两边都要不一样#
log-bin=mysql-bin #启用记录binlog
binlog-do-db=xmlgrg #记录binlog日志的数据库#
##binlog-ignore-db=mysql #不记录binlog日志的数据库#
replicate-do-db=xmlgrg#要同步的数据库#
##replicate-ignore-db=mysql #不同步的数据库#
log-slave-updates #启用更新binlog,处于master角色的时候必须。
slave-skip-errors=all #跳过不是很重要的错误信息#
sync_binlog=1 #每条自动更新,安全性高,默认是0
auto_increment_increment=2 #每次步长增长值# (红色字体的只在主主的情况下用)
auto_increment_offset=2 #起始步长值两边都要不一样#
【做同步的时候如果不选择要同步的数据库默认全部同步】
service mysqld restart #重启服务
登录mysql,创建同步用户并授权,配置master信息,启用slave:
mysql -u root -p #进入MySQL控制台
mysql> grant replication slave on *.* to ‘tongbu‘@‘192.168.137.%‘ identified by ‘tongbu‘;
mysql> flush privileges;
mysql> change master to master_host="192.168.137.75", master_port=3306, master_user="tongbu", master_password="tongbu", master_log_file="mysql-bin.000001", master_log_pos=120, master_connect_retry=10;
mysql> start slave; #启动slave
mysql> show slave status\G #查看slave 状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.137.75
Master_User: tongbu
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 421
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: xmlgrg
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: 421
Relay_Log_Space: 460
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
Master_UUID: 0496b8b2-31c8-11e6-9779-000c295bfcc7
Master_Info_File: /usr/local/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.00 sec)
测试MySQL主主服务器是否正常运行
1、进入db1服务器(192.168.137.75)
mysql -u root -p #进入MySQL控制台
use xmlgrg #进入数据库
CREATE TABLE test ( id int not null primary key,name char(20) ); #创建test表
2、进入db2服务器(192.168.137.33)
mysql -u root -p #进入MySQL控制台
use xmlgrg #进入数据库
mysql> show tables;
+------------------+
| Tables_in_xmlgrg |
+------------------+
| test |
| test22 |
+------------------+
2 rows in set (0.00 sec)
3、进入db2服务器(192.168.137.33)
mysql -u root -p #进入MySQL控制台
use xmlgrg #进入数据库
CREATE TABLE test33 ( id int not null primary key,name char(20) ); #创建test33表
4、进入db1服务器(192.168.137.75)
mysql -u root -p #进入MySQL控制台
use xmlgrg #进入数据库
mysql> show tables;
+------------------+
| Tables_in_xmlgrg |
+------------------+
| test |
| test22 |
| test33 |
+------------------+
3 rows in set (0.00 sec)
查看日志
[root@localhost data]# pwd
/usr/local/mysql/data
[root@localhost data]# ll
总用量 110624
-rw-rw----. 1 mysql mysql 56 6月 14 08:36 auto.cnf
-rw-rw----. 1 mysql mysql 12582912 6月 14 17:44 ibdata1
-rw-rw----. 1 mysql mysql 50331648 6月 14 17:44 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 6月 14 08:34 ib_logfile1
drwx------. 2 mysql mysql 4096 6月 14 08:34 mysql
-rw-rw----. 1 mysql mysql 269 6月 14 17:44 mysql-bin.000001
-rw-rw----. 1 mysql mysql 19 6月 14 17:35 mysql-bin.index
drwx------. 2 mysql mysql 4096 6月 14 17:44 osyunweidb
drwx------. 2 mysql mysql 4096 6月 14 08:34 performance_schema
drwxr-xr-x. 2 mysql mysql 4096 6月 14 08:24 test
-rw-rw----. 1 mysql mysql 6 6月 14 17:35 zabbix.xmlgrg.com.mshome.net.pid
[root@localhost data]# mysqlbinlog mysql-bin.000001
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#160614 17:35:18 server id 1 end_log_pos 120 CRC32 0x9903b903 Start: binlog v 4, server v 5.6.14-log created 160614 17:35:18 at startup
# Warning: this binlog is either in use or was not closed properly.
ROLLBACK/*!*/;
BINLOG ‘
1s9fVw8BAAAAdAAAAHgAAAABAAQANS42LjE0LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAADWz19XEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAQO5
A5k=
‘/*!*/;
# at 120
#160614 17:44:06 server id 1 end_log_pos 269 CRC32 0xff8851aa Query thread_id=1 exec_time=0 error_code=0
use `osyunweidb`/*!*/;
SET TIMESTAMP=1465897446/*!*/;
SET @@session.pseudo_thread_id=1/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
CREATE TABLE test ( id int not null primary key,name char(20) ) #这就是刚才建库语句
/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#当主库,不断发送日志过来的时候,他就不断的更新这个日志
[root@localhost data]# cat mysql-bin.index
./mysql-bin.000001
[root@localhost data]#
本文出自 “小牛---xmlgrg” 博客,请务必保留此出处http://xmlgrg.blog.51cto.com/10467412/1792144
原文地址:http://xmlgrg.blog.51cto.com/10467412/1792144