两台MySQL5.5配置主从同步

两台MySQL5.5配置主从同步

Master:10.75.65.104
Slave:10.75.65.105
主机修改配置文件:
[root@localhost ~]# vim /etc/my.cnf
在[mysqld]指令段添加以下行:
log-bin=jjx-bin
server-id=1
重启数据库服务:
[root@localhost ~]# systemctl restart mariadb
登录数据库查看是否生效:
[root@localhost ~]# mysql -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.56-MariaDB MariaDB Server

Copyright © 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

MariaDB [(none)]> show variables like “%log_bin”;
±--------------±------+
| Variable_name | Value |
±--------------±------+
| log_bin | ON |
| sql_log_bin | ON |
±--------------±------+
2 rows in set (0.01 sec)
授权从库:
MariaDB [(none)]> grant replication slave on *. * to “congji”@“10.75.65.105” identified by “123456”;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看master状态:
MariaDB [(none)]> show master status;
±---------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±---------------±---------±-------------±-----------------+
| jjx-bin.000002 | 475 |               |                      |
±---------------±---------±-------------±-----------------+
1 row in set (0.00 sec)
从机修改配置文件
[root@localhost ~]# vim /etc/my.cnf:
在[mysqld]指令段添加以下行:
server-id=2
重启数据库服务:
[root@localhost ~]# systemctl restart mariadb
登录数据库,指定master:
MariaDB [(none)]> change master to
-> master_host=“10.75.65.104”,
-> master_user=“congji”,
-> master_password=“123456”,
-> master_log_file=“jjx-bin.000002”,
-> master_log_pos=475;
Query OK, 0 rows affected (0.00 sec)
查看slave状态:
MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.75.65.104
Master_User: congji
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: jjx-bin.000002
Read_Master_Log_Pos: 475
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 527
Relay_Master_Log_File: jjx-bin.000002
Slave_IO_Running: 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: 475
Relay_Log_Space: 823
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和Slave_SQL_Running都是Yes说明正常。

验证同步:
在主库创建一个数据库:
MariaDB [(none)]> create database jjx01 charset=utf8;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| jjx01 |
| mysql |
| performance_schema |
±-------------------+

4 rows in set (0.02 sec)
在从库查看:
MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| jjx01 |
| mysql |
| performance_schema |
±-------------------+
4 rows in set (0.00 sec)
主从工作原理:
主库数据改变后,将其记录二进制bin-log日志中;
从库生成两个线程,一个I/O线程,一个SQL线程。I/O线程去请求主库的bin-log,并将得到的binlog日志写到relay(中继日志) 文件中。主库会生成一个 log dump 线程,用来给从库 I/O线程传binlog;SQL 线程,会读取relay log文件中的日志,并解析成具体操作,来实现主从的操作一致,而最终数据一致;

mysqldump备份:

只备份表,不备份数据本身:
备份jjx01数据库中的所有表,但是不会自动生成创建jjx01数据库的语句:
mysqldump -uroot -p*** jjx01 > jjx01.sql

备份数据库与表:
备份jjx01数据库中的所有表,并且会生成创建jjx01数据库的SQL语句,也就是导入时不需要先创建数据库:
mysqldump -uroot -p*** --databases jjx01 > jjx01.sql

备份多个数据库:
mysqldump -uroot -p*** --databases jjx01 mysql >jjx01_mysql.sql

备份所有数据库:
mysqldump -uroot -p*** --all-databases > all.sql
或mysqldump -uroot -p*** -A > all.sql

备份jjx01数据库,并且记录pos点:
mysqldump -uroot -p --master-data jjx01 >jjx01.sql

备份数据库,并刷新日志:
mysqldump -uroot -p --master-data --flush-logs jjx01 >jjx01.sql

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值