MySQL集群主从备份

$ mysql -V #查看mysql版本
 mysql  Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3***

1.MySQL搭建集群的原因?

a.磁盘瓶颈(数据持久化就是将数据存入磁盘也称落盘)   100W/180s = 5500
b.
//将事务分离 采用集群的方式来做。
//对比MySQL插入量和MongoDB插入量
    600事务         50000条   100数量级别

读写分离    读 --binlog--> 写

数据同步 ---binlog  //binlog 是sql语句;这是做集群的基础,错误语句不会记录到binlog
2.分库分表
每一个表里的数据是一个BTree
#创建数据库   create database db_name_0;
#使用库  use db_name_0;
#创建表  create table tab_name_0;

# 修改配置文件 /etc/mysql/my.cnf
[mysqld]
#127.0.0.1 限制只能本地连接数据库
#bind-address            = 127.0.0.1 
bind-address            = 0.0.0.0
#启动mysql服务 
sudo /etc/init.d/mysql start
#重启
sudo /etc/init.d/mysql restart
#登录 
mysql -u root -p 
# input password
#1.创建用户
mysql> create user 'aicken'@'%' identified by '123456';
#2.为用户授予权限
mysql> grant all privileges on *.* to 'aicken'@'%' identified by '123456' with grant option;
#刷新数据库 此步骤不要也行,了解有这个即可
flush privileges;
#3.重启mysql
sudo /etc/init.d/mysql restart
# 134 是master 165 是slave
###先配置master
#1.开启logbin
sudo vim /etc/mysql/my.cnf
[mysqld]
log-bin =mysql-bin
server-id = 101
#2.重启mysql
sudo /etc/init.d/mysql restart
# aicken -> mysql
#3.创建复制角色创建复制用户
mysql> create user 'replication'@'%' identified by '123456';
mysql> grant all privileges on *.* to 'replication'@'%' identified by '123456' with grant option;
mysql> flush privileges;
#4.查看master状态
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      457 |              |                  |
+------------------+----------+--------------+------------------+
文件|当前的位置
## 配置从新增server-id
[mysqld]
server-id = 102
#2.重启mysql
sudo /etc/init.d/mysql restart
mysql -u root -p
# 改变数据库的状态,不是事务语句,不会记录到binlog文件
mysql> change master to master_host='192.168.215.134',master_port=3306, master_user='replication', master_password='123456',master_log_file='mysql-bin.000001', master_log_pos=457;
# 开始slave
mysql> start slave;
# 查看slave 状态 \G竖排
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.215.134
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 457
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             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: 457
              Relay_Log_Space: 410
              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: 101
1 row in set (0.00 sec)
# 现在master和slave可以同步了。

你可能感兴趣的:(MySQL集群主从备份)