mysql设置主从复制

环境准备。2台虚拟机。ubuntu 18.04

主服务器: 10.13.14.110
副服务器: 10.13.14.111

1、在两台服务器上,都安装好mysql。

apt update && apt install mysql-server -y

2、在主服务器上,进行操作。

vi  /etc/mysql/mysql.conf.d/mysqld.cnf     #编辑数据库配置文件
注释掉   bind=127.0.0.1                   #43行左右
取消注释  server-id               = 1      #84行左右
取消注释log_bin       = /var/log/mysql/mysql-bin.log #85行左右
保存成功之后,重启mysql
systemctl restart mysql

3、在副服务器上,进行操作。

vi  /etc/mysql/mysql.conf.d/mysqld.cnf     #编辑数据库配置文件
注释掉   bind=127.0.0.1                   #43行左右
取消注释  server-id   = 1     #84行左右
然后把1 改成 2或者其他非1数字。
保存成功之后,重启mysql
systemctl restart mysql

4、在主服务器上配置mysql。

mysql -u root -p   #进入mysql
#创建账号
mysql> grant replication slave on *.* to 'foxi'@'%' identified by 'foxi';
Query OK, 0 rows affected, 1 warning (0.01 sec)
#刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

#查看状态。注意,几下下面的file 和Position
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      593 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

5、在副服务器上。

mysql -u root -p   #进入数据库
#配置主服务器信息。注意 把这里的用户密码,以及file/pos 改成自己的
mysql> change master to master_host='10.13.14.110', master_port=3306, master_user='jiangcuo', master_password='jiangcuo', master_log_file='mysql-bin.000001', master_log_pos=593;
mysql> start slave;   #开启副服务器
mysql> show slave status\G   #查看服务器信息

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.13.14.110
                  Master_User: jiangcuo
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 593
               Relay_Log_File: mysql2-relay-bin.000002
                Relay_Log_Pos: 320
        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: 593
              Relay_Log_Space: 528
              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_UUID: ac31fac4-b394-11ea-ae48-b6387fc5fbd3
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind:
      Last_IO_Error_Timestamp:
     Last_SQL_Error_Timestamp:
               Master_SSL_Crl:
           Master_SSL_Crlpath:
           Retrieved_Gtid_Set:
            Executed_Gtid_Set:
                Auto_Position: 0
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
1 row in set (0.00 sec)

6、在主服务器上,创建一个数据库sql;

create database sql;

7、在副服务器上,验证是否同步。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sql                |   #已同步
| sys                |
+--------------------+
5 rows in set (0.01 sec)

版权声明:
作者:佛西
链接:https://foxi.buduanwang.vip/linux/582.html/
文章版权归作者所有,未经允许请勿转载
如需获得支持,请点击网页右上角
THE END
分享
二维码
< <上一篇
下一篇>>