安装前要先把杀毒软件,实时防护全都关掉。还有vpn。关掉linux系统的防火墙。
一、主库操作
1)、确保/etc/my.cnf中有如下参数
[mysqld]
log_bin=mysql-bin 启动二进制文件
server_id=1 服务器ID
2)、登录mysql,在mysql中添加一个backup的账号,并授权给从服务器。
mysql> grant replication slave on *.* to 'backup'@'%' identified by '3cqscbrOnly1';
Query OK, 0 rows affected (0.02 sec)
3)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 263 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
二、数据迁移
1、第一种方法使用mysqldump
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.05 sec)
mysqldump -uroot -p --databases meitao shifei > /backup/a.sql
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
2、第二种方法使用Inobackupex
主库操作
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --safe-slave-backup --no-timestamp /backup/
将/back整个目录scp到从库服务器
从库操作
先关闭从库,并清除/opt/mysql/data目录下的内容
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --apply-log --use-memory=1GB /backup/
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --copy-back --use-memory=1GB /backup/
Ps:用 innobackupex 备份数据时,–apply-log 处理过的备份数据里有两个文件说明该备份数据对应的 binlog 的文件名和位置。但有时这俩文件说明的位置可能会不同。
1 对于纯 InnoDB 操作,备份出来的数据中上述两个文件的内容是一致的
2 对于 InnoDB 和非事务存储引擎混合操作,xtrabackup_binlog_info 中所示的 position 应该会比 xtrabackup_pos_innodb 所示的数值大。此时应以 xtrabackup_binlog_info 为准;而后者和 apply-log 时 InnoDB recovery log 中显示的内容是一致的,只针对 InnoDB 这部分数据。
在从库上看xtrabackup_binlog_info的file和POSition,xtrabackup_binlog_info文件在scp过来的备份目录里。
三、从库上操作
1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10。修改之后如下所示:
[mysqld]
log_bin=mysql-bin 启动二进制文件
server_id=10 服务器ID
relay_log=relay-log-bin
log_slave_updates=1
read_only=1
----------------------------
2)、重启mysql服务。
[root@localhost ~]# mysqladmin -uroot -pm123 shutdown
[root@localhost ~]# mysqld_safe --user=mysql &
3)、登录mysql,执行如下语句
mysql> change master to master_host='192.168.0.10',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=263;
4)、启动slave同步。
mysql> start slave;
5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。
mysql> show slave status\G
验证配置是否正常,mysql主从能否正常复制。
在主数据库上新建一个库,并且在库中写一个表和一些数据。
mysql> create database orcl;
Query OK, 1 row affected (0.01 sec)
mysql> use orcl;
Database changed
mysql> create table t1(a int(10),b int(10));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(1,1);
Query OK, 1 row affected (0.00 sec)
在从数据库中验证一下,是否正常复制到数据。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| orcl |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use orcl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_orcl |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
***停库先停从,再停主。启动时,先起主,再起从。
现在是异步同步,下面是配置半同步。
四、配置半同步机制
mysql> show variables like 'plugin_dir';
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| plugin_dir | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
-bash-4.1$ cd /usr/lib64/mysql/plugin/
加载半同步插件
mysql> 主库
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'
从库
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'
查看插件是否处于正常状态
mysql> show plugins
设置半同步参数
--建议写进my.cnf文件,以免每次重启MySQL都要设置参数
主库
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 3000;
从库
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
重启从库IO线程
mysql> STOP SLAVE IO_THREAD;
mysql> START SLAVE IO_THREAD;
只要启动过程中没有出现错误,就表示配置成功了
五、监控半同步复制环境
mysql>show status like 'rpl_semi_sync%';
注释:
MySQL的半同步插件,会将一些与同步相关的统计信息写入到状态变量中,这些状态量以Rpl_semi_sync_*开头
* Rpl_semi_sync_master_client 显示当前处于半同步模式的slave节点数量
* Rpl_semi_sync_master_status 表示当前Master节点是否启用了半同步模式
* Rpl_semi_sync_master_no_tx 显示当前未成功发现送到slave节点的事务数量
* Rpl_semi_sync_master_yes_tx 显示已成功发送到slave节点的事务数量
一、主库操作
1)、确保/etc/my.cnf中有如下参数
[mysqld]
log_bin=mysql-bin 启动二进制文件
server_id=1 服务器ID
2)、登录mysql,在mysql中添加一个backup的账号,并授权给从服务器。
mysql> grant replication slave on *.* to 'backup'@'%' identified by '3cqscbrOnly1';
Query OK, 0 rows affected (0.02 sec)
3)、查询主数据库状态,并记下FILE及Position的值,这个在后面配置从服务器的时候要用到。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 263 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
二、数据迁移
1、第一种方法使用mysqldump
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.05 sec)
mysqldump -uroot -p --databases meitao shifei > /backup/a.sql
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
2、第二种方法使用Inobackupex
主库操作
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --safe-slave-backup --no-timestamp /backup/
将/back整个目录scp到从库服务器
从库操作
先关闭从库,并清除/opt/mysql/data目录下的内容
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --apply-log --use-memory=1GB /backup/
innobackupex --defaults-file=/opt/mysql/my.cnf --user=root --password= --copy-back --use-memory=1GB /backup/
Ps:用 innobackupex 备份数据时,–apply-log 处理过的备份数据里有两个文件说明该备份数据对应的 binlog 的文件名和位置。但有时这俩文件说明的位置可能会不同。
1 对于纯 InnoDB 操作,备份出来的数据中上述两个文件的内容是一致的
2 对于 InnoDB 和非事务存储引擎混合操作,xtrabackup_binlog_info 中所示的 position 应该会比 xtrabackup_pos_innodb 所示的数值大。此时应以 xtrabackup_binlog_info 为准;而后者和 apply-log 时 InnoDB recovery log 中显示的内容是一致的,只针对 InnoDB 这部分数据。
在从库上看xtrabackup_binlog_info的file和POSition,xtrabackup_binlog_info文件在scp过来的备份目录里。
三、从库上操作
1)、确保/etc/my.cnf中有log-bin=mysql-bin和server-id=1参数,并把server-id=1修改为server-id=10。修改之后如下所示:
[mysqld]
log_bin=mysql-bin 启动二进制文件
server_id=10 服务器ID
---------------------------
以下的参数可以不填
sync_binlog=1relay_log=relay-log-bin
log_slave_updates=1
read_only=1
----------------------------
2)、重启mysql服务。
[root@localhost ~]# mysqladmin -uroot -pm123 shutdown
[root@localhost ~]# mysqld_safe --user=mysql &
3)、登录mysql,执行如下语句
mysql> change master to master_host='192.168.0.10',master_user='backup',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=263;
4)、启动slave同步。
mysql> start slave;
5)、检查主从同步,如果您看到Slave_IO_Running和Slave_SQL_Running均为Yes,则主从复制连接正常。
mysql> show slave status\G
验证配置是否正常,mysql主从能否正常复制。
在主数据库上新建一个库,并且在库中写一个表和一些数据。
mysql> create database orcl;
Query OK, 1 row affected (0.01 sec)
mysql> use orcl;
Database changed
mysql> create table t1(a int(10),b int(10));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t1 values(1,1);
Query OK, 1 row affected (0.00 sec)
在从数据库中验证一下,是否正常复制到数据。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| orcl |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> use orcl;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+----------------+
| Tables_in_orcl |
+----------------+
| t1 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from t1;
+------+------+
| a | b |
+------+------+
| 1 | 1 |
+------+------+
1 row in set (0.00 sec)
***停库先停从,再停主。启动时,先起主,再起从。
现在是异步同步,下面是配置半同步。
四、配置半同步机制
mysql> show variables like 'plugin_dir';
+---------------+--------------------------+
| Variable_name | Value |
+---------------+--------------------------+
| plugin_dir | /usr/lib64/mysql/plugin/ |
+---------------+--------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
-bash-4.1$ cd /usr/lib64/mysql/plugin/
加载半同步插件
mysql> 主库
INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'
从库
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'
查看插件是否处于正常状态
mysql> show plugins
设置半同步参数
--建议写进my.cnf文件,以免每次重启MySQL都要设置参数
主库
mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1;
mysql> SET GLOBAL rpl_semi_sync_master_timeout = 3000;
从库
mysql> SET GLOBAL rpl_semi_sync_slave_enabled = 1;
重启从库IO线程
mysql> STOP SLAVE IO_THREAD;
mysql> START SLAVE IO_THREAD;
只要启动过程中没有出现错误,就表示配置成功了
五、监控半同步复制环境
mysql>show status like 'rpl_semi_sync%';
注释:
MySQL的半同步插件,会将一些与同步相关的统计信息写入到状态变量中,这些状态量以Rpl_semi_sync_*开头
* Rpl_semi_sync_master_client 显示当前处于半同步模式的slave节点数量
* Rpl_semi_sync_master_status 表示当前Master节点是否启用了半同步模式
* Rpl_semi_sync_master_no_tx 显示当前未成功发现送到slave节点的事务数量
* Rpl_semi_sync_master_yes_tx 显示已成功发送到slave节点的事务数量