mysql主从配置

时间:2021-09-15
本文章向大家介绍mysql主从配置,主要包括mysql主从配置使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

根据之前的二进制免编译安装mysql

安装俩台mysql做好基础配置(配置网络,关闭selinux ,关闭防火墙)

1.mysql主配置

vim  /etc/my.cnf(还需要添加一条 log_bin=liwei)

basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 100
socket = /tmp/mysql.sock

 /etc/init.d/mysqld restart(重启服务)

export PATH=$PATH:/usr/local/mysql/bin(配置环境变量最好是在/etc/profile添加)

mysqldump -uroot mysql > /tmp/mysql.sql(备份数据库)
mysql -uroot -e "create database repl"(创建一个库保存数据)

mysql -uroot liwei < /tmp/mysql.sql(恢复成新的库)

grant replication slave on *.* to 'repl' @192.168.100.101 identified by '123456';(赋予权限)

 flush tables with read lock;(把表锁住)

show master status;(显示主机状态)

2.mysql从配置

vim /etc/profile(配置玩用source命令刷新一下参数)

export PATH=$PATH:/usr/local/mysql/bin

vim /etc/my.cnf

basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 101
socket = /tmp/mysql.sock

/etc/init.d/mysqld restart(重启服务)

scp /tmp/mysql.sql root@192.168.100.101:/tmp/(在主上把这个文件复制到从上)(复制完之后到从下的/data/mysql下把UUID的文件(auto.cnf)删掉)

用ls -la 命令查看一下是否一样

mysql -uroot -e "create database repl"(创建和主一样的库)

change master to master_host='192.168.100.100',master_user='repl',master_password='123456',master_log_file='liwei.000002',master_log_pos=699462;

unlock tables;(在主上解锁表)

show slave status\G;(从上查看)

*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.100.100
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: liwei.000002
          Read_Master_Log_Pos: 699462
               Relay_Log_File: localhost-relay-bin.000004
                Relay_Log_Pos: 364
        Relay_Master_Log_File: liwei.000002
             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: 699462
              Relay_Log_Space: 700
              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: 100
                  Master_UUID: 15b120fa-1542-11ec-bd79-000c291c76ba
             Master_Info_File: /data/mysql/master.info
                    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
           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
1 row in set (0.00 sec)

测试从主:

select count(*) from db;(进入主数据库)

select count(*) from db;(进入从数据库查看)

truncate table db;(进入主数库删除数据)

select count(*) from db;(进入从数据库查看)

原文地址:https://www.cnblogs.com/liwei-1-1-1-1/p/15271570.html