mysql主从配置

mysql主从配置

mysql安装

mysql主从配置

确保从数据库与主数据库里的数据一样

为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中

//先查看主库有哪些库
[root@httpd ~]# mysql -uroot -p'drhandhjl123!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hjl                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


//再查看从库有哪些库
[root@dabaozi ~]# mysql -uroot -p'drhandhjl123!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+


//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec) 
//此锁表的终端必须在备份完成以后才能退出

//备份主库并将备份文件传送到从库
[root@httpd ~]# mysqldump -uroot -pdrhandhjl123! --all-databases > /opt/all-20220702.sql
root@httpd ~]# ls /opt/
all-20220702.sql  data  dnf.conf  myrepo
[root@httpd ~]# scp /opt/all-20220702.sql root@192.168.140.140:/opt/
root@192.168.140.140's password: 
all-20220702.sql                                     100%  856KB  37.4MB/s   00:00    
 
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye


//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@dabaozi ~]# mysql -uroot -p'drhandhjl123!' < /opt/all-20220702.sql 
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@dabaozi ~]# mysql -uroot -p'drhandhjl123!' -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hjl                |
| mysql              |
| performance_schema |
| sys                |

在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'repl'@'192.168.140.136' identified by 'repl123';
Query OK, 0 rows affected (0.01 sec)


mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.140.140';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
配置主数据库
[root@httpd ~]# vim /etc/my.cnf
//在[mysqld]这段的后面加上如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-bin=mysql-bin   //启用binlog日志
server-id=1     //数据库服务器唯一标识符,主库的server-id值必须比从库的大

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 


//重启mysql服务
[root@httpd ~]# systemctl restart mysqld
[root@httpd ~]# ss -antl
State       Recv-Q Send-Q      Local Address:Port                     Peer Address:Port
LISTEN      0      128                     *:22                                  *:*
LISTEN      0      100             127.0.0.1:25                                  *:*
LISTEN      0      80                     :::3306                               :::* 


//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      172 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
配置从数据库
[root@ ~]# vim /etc/my.cnf
//添加如下内容
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server-id=2     //设置从库的唯一标识符,从库的server-id值必须小于主库的该值
relay-log=mysql-relay-bin       //启用中继日志relay-log

symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


//重启从库的mysql服务
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q           Local Address:Port                          Peer Address:Port
LISTEN     0      128                          *:22                                       *:*
LISTEN     0      100                  127.0.0.1:25                                       *:*
LISTEN     0      80                          :::3306                                    :::*




//配置并启动主从复制
mysql> CHANGE MASTER TO
    -> MASTER_HOST='192.168.140.140',
    -> MASTER_USER='repl',
    -> MASTER_PASSWORD='repl123',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=172;
Query OK, 0 rows affected, 2 warnings (0.33 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)


//查看从服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.140.137
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql-relay-bin.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes      //此处必须为Yes
            Slave_SQL_Running: Yes      //此处必须为Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:  
3.2.5 测试验证

在主服务器的student库的bj2表中插入数据:

mysql> select * from bj2;
Empty set (0.00 sec)

mysql> insert into bj2 values (1,'sean',20),(2,'tom',23),(3,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from bj2;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | sean  |   20 |
|  2 | tom   |   23 |
|  3 | jerry |   30 |
+----+-------+------+
3 rows in set (0.00 sec)

在从数据库中查看数据是否同步:

mysql> use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from bj2;
+----+-------+------+
| id | name  | age  |
+----+-------+------+
|  1 | sean  |   20 |
|  2 | tom   |   23 |
|  3 | jerry |   30 |
+----+-------+------+
3 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值