Mysql主从
mysql主从复制
	主IP:
		10.0.0.6
		
	从IP:
		10.0.0.8
		
修改主的my.cnf配置文件
	添加到[mysqld]语句块里
		vim /etc/my.cnf
			添加以下内容
			log-bin=mysql-bin
			server-id=1
				解释:
					log-bin=mysql-bin
						启用二进制日志,binlog日志文件的开头名称
							
					
					server-id=1
						服务的唯一标识符,如主的ID=1那么从的ID就不能=1,应该从2...100不等的数字
						为当前节点设置一个全局惟一的ID号
						server-id的取值范围
						1 to 4294967295 (>= MariaDB 10.2.2),默认值为1
					    log-0 to 4294967295 (<= MariaDB 10.2.1),默认值为0,如果从节点为0,所有master都将拒绝此slave的	
						
					basename=master  #可选项,设置datadir中日志名称,确保不依赖主机名

在主的上面创建同步的授权账号
创建有复制权限的用户账号
	GRANT REPLICATION SLAVE on *.* to 'rsync'@'10.0.0.8' identified by '123456';
		解释:
			REPLICATION SLAVE
				授权给slave只读权限 
				
			'rsync'@'10.0.0.8'
				同步的账号和从的主机的IP
				
			IDENTIFIED BY '123456';
				设置同步的密码是多少 123456
				
查看主二进制日志的文件和位置开始进行同步
	输入show master status;查看主的二级制日志的文件名和位置开始的位置
	MariaDB [(none)]> show master status;
		返回结果:
		+------------------+----------+--------------+------------------+
		| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
		+------------------+----------+--------------+------------------+
		| mysql-bin.000003 |      538 |              |                  |
		+------------------+----------+--------------+------------------+
		1 row in set (0.00 sec)
		解释:
			mysql-bin.000003
				二进制日志的文件名称,每个人的二进制文件的名称不一定一样
				
			538
				二进制文件开始的位置
				
			
	
从节点配置
	启动中继日志
	添加到[mysqld]语句块里
		vim /etc/my.cnf			
			添加以下内容			
			log-bin=mysql-bin		
			server-id=2			
				解释:			
					log-bin=mysql-bin			
						启用二进制日志,binlog日志文件的开头名称			
										
								
					server-id=2			
						服务的唯一标识符,如主的ID=1那么从的ID就不能=1,应该从2...100不等的数字			
						为当前节点设置一个全局惟一的ID号			
						server-id的取值范围			
						1 to 4294967295 (>= MariaDB 10.2.2),默认值为1			
					    log-0 to 4294967295 (<= MariaDB 10.2.1),默认值为0,如果从节点为0,所有master都将拒绝此slave的			
									
					
					选项添加,课不加
					read_only=ON 
						#设置数据库只读,针对supper user无效
						
					relay_log=relay-log 
						#relay log的文件路径,默认值hostname-relay-bin
						
					relay_log_index=relay-log.index  
						#默认值hostname-relay-bin.index
	
进入数据库后添加主节点的IP账号密码等信息
		CHANGE MASTER TO MASTER_HOST='10.0.0.6', MASTER_USER='rsync', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000003', MASTER_LOG_POS=538;
			解释:
				MASTER_HOST='10.0.0.6',
					主的数据库的IP地址
					
				MASTER_USER='rsync',
					主的数据库授权同步的账号
					
				MASTER_PASSWORD='123456',
					主的数据库授权同步密码
					
				MASTER_LOG_FILE='mysql-bin.000003',
					主的数据库的二进制日志文件名称
					
				MASTER_LOG_POS=538;
					主的二进制日志文件起始同步位置
					
启动slave从的同步进程
	start slave;
	
查看启动状态
	show slave status\G
	返回结果:
		*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.6
                  Master_User: rsync
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 538
               Relay_Log_File: centos7-relay-bin.000003
                Relay_Log_Pos: 555
        Relay_Master_Log_File: mysql-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: 538
              Relay_Log_Space: 866
              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
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)

		如果这两项都为yes说明同步是成功的
			Slave_IO_Running: Yes
			Slave_SQL_Running: Yes	
				
				
				
				
				
				
				
				
				
				
				
				
			
					
					


本文 暂无 评论

Top