mysql之主从配置实现

我使用的是两台centos7虚拟机来做实验的,主服务器ip为192.168.2.128,从服务器ip为192.168.2.130
安装mysql就不用说了吧,不对,我们需要安装的是mariadb,命令行安装yum install mariadb mariadb-server -y就可以了
主,从服务器安装好mariabd后,开启mysql服务,修改密码(应该是这条命令mysql_secure_installtion),修改好密码后进入mysql,命令mysql -uroot -p 你的密码

在主服务器上进行下列操作 :

mysql> GRANT all privileges ON *.* TO 'repl'@'192.168.2.%' IDENTIFIED BY 'mysql';   #新建用户,并授权,注意要是全部权限(其实这个权限过大了),或者复制权限

修改my.cnf,vi /etc/my.cnf
在[mysqld]下面增加下面两行代码
server_id=1 #给数据库服务的唯一标识,我一般设置为服务器Ip最后一个数 
log-bin=master-bin #设置为master
保存退出并重启MySQL服务
查看日志
mysql> SHOW MASTER STATUS;
+——————-+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————-+———-+————–+——————+
| master-bin.000001 | 1285 | | |
+——————-+———-+————–+——————+
1 row in set (0.00 sec)
显示上面的内容说明配置正确了

记录File | Position下面的那两个值了,在从服务器配置时我们需要用到

从服务器配置如下 :
也是修改my.cnf,在[mysqld]下面增加如下内容
server_id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
重启mysql服务器
进入mysql,执行下面的命令,其中Master服务器产生的File | Position对应下面的master_log_file和master_log_pos值,填写完之后在主服务器执行一下SHOW MASTER STATUS;确保这两个值是正确对应的

change master to master_host='192.168.2.128', //Master 服务器Ip
master_port=3306,
master_user='repl',
master_password='mysql',
master_log_file='master-bin.000001',
master_log_pos=1285;

然后start slave; 

接着在从服务器上shwo slave statusG;如果返回结果跟下面差不多(主要就是Slave_IO_Running和Slave_SQL_Running这两个值一定要是yes才是配置正确的)

MariaDB [(none)]> show slave statusG;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.128
                  Master_User: kali
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 245
               Relay_Log_File: slave-relay-bin.000005
                Relay_Log_Pos: 530
        Relay_Master_Log_File: master-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: 245
              Relay_Log_Space: 1109
              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)

补充:下面是我再配置过程中遇到的一些问题

一:我的第一个错误是执行start slave后报

ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

上面这个错误是因为我master_log_file=’master-bin.000001’这里填错了,没有对应master服务器,master_log_pos的值不对应也会报错 
我因为配置了很多次主从都没有成功,一直报上面那个错误,所以可以使用stop slave;然后reset slave;重置slave配置,接着在执行那条change master命令,最后执行start slave;就不会报错了

二:show slave status G;显示不正确
mysql >show slave status G; 
不是显示 
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes 
而是显示 
Slave_IO_Running: Connecting 
Slave_SQL_Running: Yes 
这表示我们还没有配置正确 
后来我试了试远程用reql用户登录主服务器myql,发现无法登录,后来上网查了查资料,发现原来是防火墙阻止了,那么让我们简单粗暴的解决问题吧,systmctl stop firewalld 

后来再次实验遇到过一个很郁闷的问题就是一直显示Slave_IO_Running: Connecting 

后来我发现创建用户时,我是这么创建的
grant all privileges on test.* to bp@’192.168.245.140’ identified by ‘123456’;
注意到有什么不一样的地方不。其实语句本身没有错误,但是用在这里却不行,会导致从库出错,从库必须对全部的数据库据都有全部权限,而我这里只授权了test里面的表权限,坑人啊

报slave_io running:no 可能是防火墙问题,可以尝试关闭mysql 主服务器的防火墙 

原文地址:https://www.cnblogs.com/biaopei/p/7730567.html