MySQL主从介绍 , 准备工做,配置主,配置从, 测试主从同步

MySQL主从介绍

  • MySQL主从又叫作Replication、AB复制。简单讲就是A和B两台机器作主从后,在A上写数据,另一台B也会跟着写数据,二者数据实时同步的
  • MySQL主从是基于binlog的,主上须开启binlog才能进行主从。
  • 主从过程大体有3个步骤

1)主将更改操做记录到binlog里html

2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里mysql

3)从根据relaylog里面的sql语句按顺序执行linux

主上有一个log dump线程,用来和从的I/O线程传递binlogsql

从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另一个SQL线程用来把relaylog里面的sql语句落地服务器

准备工做

  • 准备两台服务器作主和从,两台服务器都要安装mysql并启动

配置主

  • 修改my.cnf,增长server-id=128和log_bin=aminglinux1
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=128
log_bin=aminglinux1

改完重启服务socket

[root@aminglinux-01 ~]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@aminglinux-01 ~]#

aminglinux1.index,aminglinux1.000001必定要有这两个,这两个很关键。主从要用到。ide

[root@aminglinux-01 ~]# ls -lt /data/mysql/
总用量 110636
-rw-rw----. 1 mysql mysql 50331648 11月  9 14:20 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 11月  9 14:20 ibdata1
-rw-rw----. 1 mysql mysql    17384 11月  9 14:20 aminglinux-01.err
-rw-rw----. 1 mysql mysql        5 11月  9 14:20 aminglinux-01.pid
-rw-rw----. 1 mysql mysql       21 11月  9 14:20 aminglinux1.index
-rw-rw----. 1 mysql mysql      120 11月  9 14:20 aminglinux1.000001
-rw-rw----. 1 mysql mysql       56 9月  27 09:52 auto.cnf
drwx------. 2 mysql mysql     4096 9月  26 21:00 mysql
drwx------. 2 mysql mysql     4096 9月  26 21:00 performance_schema
-rw-rw----. 1 mysql mysql 50331648 9月  26 21:00 ib_logfile1
drwx------. 2 mysql mysql        6 9月  26 21:00 test
[root@aminglinux-01 ~]#
  • 建立一个库 mysql -uroot -paminglinux -e "create database aming"
[root@aminglinux-01 ~]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
  • 建立一个用户,用来作主从同步数据的用户

mysql -uroot -paminglinux 进入mysql学习

建立用户 replication slave on . to 'repl'@slave_ip' identified by 'password';this

mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'repl'@'192.168.245.130' identified by 'aminglinux111';
Query OK, 0 rows affected (0.00 sec)

mysql>

锁一下,不让主继续写了。让二者保持一致。flush tables with read lock;.net

show master status;

mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File               | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000002 |      495 |              |                  |                   |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

到这个目录下,坐下备份

[root@aminglinux-01 mysql]# cd /data/mysql/
[root@aminglinux-01 mysql]# ls
aming              aminglinux-01.pid   aminglinux1.000002  auto.cnf  ib_logfile0  mysql               test
aminglinux-01.err  aminglinux1.000001  aminglinux1.index   ibdata1   ib_logfile1  performance_schema
[root@aminglinux-01 mysql]#

ls /tmp/*sql 而后把这个目录下全部的sql文件拷贝到从上去

配置从

  • 查看my.cnf,配置server-id=130,要求和主不同。logbin不须要配置了,只有主须要配置
  • 修改完配置文件后,启动或者重启mysqld服务
  • 能够先建立aming库
mysql> create database aming;
Query OK, 1 row affected (0.01 sec)
  • 而后把主上的库文件capy到从上让两边库保持一致
  • 登录从的mysql

stop slave;

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

实现主从很是关键的一部,change master to master_host='', master_user='repl', master_password='', master_log_file='', master_log_pos=xx,

下面的数值是以前主上show master status;获得的

mysql> change master to master_host='192.168.245.128', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000002', master_log_pos=495;

start slave;

show slave status\G查看是否成功,两个yes表明成功

Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

还要到主上执行 unlock tables

#扩展学习

相关文章
相关标签/搜索