vlambda博客
学习文章列表

第十一期:基于两台Mysql5.5配置主从同步并进行数据库备份

一.  Mysql主从同步

1.1  Mysql主从概念

Mysql的主从复制,是用来建立一个和主数据库完全一样的数据库环境,从库会同步主库的所有数据,可以轻松实现故障转移。

1.2  Mysql主从主要作用

·实现数据备份

·基于数据备份实现故障转移

·基于数据备份,实现读写分离

1.3  常见Mysql主从架构

一主一从

一主多从

互为主从

1.4  Mysql主从

1.4.1  主从工作原理



1.4.2  环境准备

master:192.168.193.102

slave:192.168.193.101

1.4.3  Master端配置

#yum安装mariadb

[[email protected]]# yum install mariadb mariadb-server –y

#修改配置文件,在[mysqld]指令段添加以下行

[root@node2 ~]#vim /etc/my.cnf

log-bin=wwmgc-bin

server-id=1

#重启数据库服务

[root@node2 ~]#systemctl start mariadb

#查看mysql进程

[root@node2 ~]#ps -ef | grep mysqld

#查看mysql端口

[root@node2 ~]#netstat -ntlp | grep 3306

tcp        0     0 0.0.0.0:3306           0.0.0.0:*               LISTEN      2453/mysqld 

#查看配置是否生效

[root@node2 ~]#ps -ef | grep mysqld

mysql      2266     1  0 10:17 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe--basedir=/usr

mysql      2453  2266  0 10:17 ?        00:00:00 /usr/libexec/mysqld--basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin--log-error=/var/log/mariadb/mariadb.log--pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock

root       2515  1345  0 10:17 pts/0    00:00:00 grep --color=auto mysqld

[root@node2 ~]#netstat -ntlp | grep 3306

tcp        0     0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2453/mysqld        

[root@node2 ~]#mysql

Welcome to theMariaDB monitor.  Commands end with ; or\g.

Your MariaDBconnection id is 2

Server version:5.5.65-MariaDB MariaDB Server

 

Copyright (c)2000, 2018, 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.00 sec)

 

1.4.4  授权从库

MariaDB[(none)]> grant replication slave on *.* to"wwmgc"@"192.168.193.101" identified by "123456";

Query OK, 0 rowsaffected (0.01 sec)

 

MariaDB [(none)]>flush privileges;

Query OK, 0 rowsaffected (0.00 sec)

1.4.5  查看master状态

MariaDB[(none)]> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB |Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

|wwmgc-bin.000003 |      709 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set(0.00 sec)

1.4.6  slave端配置

MariaDB[(none)]> change master to

    ->master_host="192.168.193.102",

    -> master_user="wwmgc",

    -> master_password="123456",

    ->master_log_file="wwmgc-bin.000003",

-> master_log_pos=709;

              QueryOK, 0 rows affected (0.02 sec)

1.4.7  查看slave状态

MariaDB[(none)]> slave start;

Query OK, 0 rowsaffected (0.01 sec)

 

MariaDB[(none)]> show slave status\G

***************************1. row ***************************

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.193.102

                  Master_User: wwmgc

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: wwmgc-bin.000003

          Read_Master_Log_Pos: 709

               Relay_Log_File:mariadb-relay-bin.000002

                Relay_Log_Pos: 529

        Relay_Master_Log_File: wwmgc-bin.000003

             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: 709

              Relay_Log_Space: 825

              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)

1.4.8  验证数据同步

#在主库创建一个数据库:

MariaDB[(none)]> create database wwmgc charset=utf8;

Query OK, 1 rowaffected (0.01 sec)

 

MariaDB[(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

|information_schema |

| mysql              |

|performance_schema |

| test               |

| wwmgc              |

+--------------------+

5 rows in set(0.00 sec)

 

#在从库查看,发现从库也多了一个wwmgc的数据库:

 

MariaDB[(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

|information_schema |

| gmall              |

| mysql              |

|performance_schema |

| wwmgc              |

+--------------------+

5 rows in set(0.00 sec)

 

 

####实现了mysql的主从同步

1.5  Mysqldump备份

1.5.1  只备份表,不备份数据本身:

#备份wwmgc中的所有表,但是不会自动生成创建wwmgc数据库中的语句

              [root@master~]# mysqldump -uroot -p123 wwmgc > wwmgc.sql

1.5.2  备份数据库与表

#备份wwmgc中的数据库中的所有表,并且会生成创建wwmgc数据库的SQL语句,也就是导入时不需要先创建数据库:

[root@master ~]#mysqldump -uroot -p123 --databases wwmgc > wwmgc.sql

1.5.3  备份多个数据库

[root@master ~]#mysqldump -uroot -p123 --databases wwmgc mysql > wwmgc_mysql.sql

1.5.4  备份所有数据库

[root@master ~]#mysqldump -uroot -p123 --all-databases > all.sql

1.5.5  备份wwmgc数据库,并记录pos

[root@node2 ~]#mysqldump -uroot -p --master-data mysql > mysql.sql

1.5.6  备份数据库,并刷新日志

[root@node2 ~]#mysqldump -uroot -p --master-data --flush-logs mysql > mysql.sql




不论你在什么时候开始,重要的是开始之后就不要停止!

--end--












了解更多请关注吧


点个小花花,让他们知道你“在看