2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > mysql数据库实现主从复制

mysql数据库实现主从复制

时间:2022-10-10 18:33:50

相关推荐

mysql数据库实现主从复制

服务器准备

主服务器:192.168.93.103

从服务器:192.168.93.102

主服务器操作

修改配置文件

[root@CentOS7 ~]#vim /etc/f[mysqld]server_id=103 //指定一个服务id,如果不写这里默认为1log_bin=/data/mysql/bin/mysql-bin //必须启动二进制日志binlog_format=row //建议使用行row记录日志innodb_file_per_tabledatadir=/var/lib/mysql

创建主从复制用户

MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.93.%'identified by 'centos';

查看主服务器的二进制日志,记录要复制的位置

[root@node-103 ~]#mysql -e 'show master logs';+------------------+-----------+| Log_name | File_size |+------------------+-----------+| mysql-bin.000002 | 288 || mysql-bin.000003 | 288 || mysql-bin.000004 | 442 || mysql-bin.000005 | 245 |+------------------+-----------+

如何解决主从复制一台已存在大量数据的mysql服务器?

问题描述:如果一台mysql主服务器已经运行了一段时间,主服务器的数据量非常大,而主从复制时间会很长,对主服务器压力过大

解决办法:先在主服务器上创建完全备份,还原到从服务器上后,再做主从复制

完全备份数据库

[root@CentOS7 ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql

查看记录二进制日志中要复制的位置信息

[root@CentOS7 ~]#vim /data/backup/all.sql CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000018', MASTER_LOG_POS=245;

在从服务器需要做的操作:

修改配置文件

将主服务器完全备份的数据库文件all.sql拷贝至从服务器

[root@CentOS7 ~]#scp 192.168.93.102:/data/backup/all.sql /data/backup

修改/data/backup/all.sql文件里完全备份位置语句:

CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000018',MASTER_LOG_POS=245;

修改为:

vim /data/backup/all.sqlCHANGE MASTER TOMASTER_HOST='192.168.93.103', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000018',MASTER_LOG_POS=245;

然后导入all.sql文件

[root@CentOS7 ~]# mysql < all.sql

启动主从同步线程

MariaDB [(none)]> start slave;

从服务器操作

修改配置文件

[root@CentOS7 ~]# vim /etc/f[mysqld]server_id=102 //指定从服务器的服务id,不能不写,默认为1,此id不能与其他服务器冲突read_only=ON //设置数据库只读

使用有复制权限的用户账号连接至主服务器,配置同步信息

MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.93.103', //远程主服务器IPMASTER_USER='repluser', //主从复制的用户名MASTER_PASSWORD=’centos’, //密码MASTER_PORT=3306, //端口号MASTER_LOG_FILE='mysql-bin.000017', //要复制的主服务器的二进制文件名 MASTER_LOG_POS=245; //要开始复制的位置

启动复制线程

MariaDB [(none)]> start slave;Query OK, 0 rows affected (0.00 sec)

显示线程列表

MariaDB [(none)]> show processlist;+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+| Id | User | Host| db | Command | Time | State | Info | Progress |+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+| 3 | root | localhost | NULL | Query |0 | NULL | show processlist | 0.000 || 6 | system user | | NULL | Connect | 264 | Waiting for master to send event | NULL | 0.000 || 7 | system user | | NULL | Connect | -27302 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 |+----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+3 rows in set (0.00 sec)

显示主服务器的状态信息

MariaDB [(none)]> show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.93.103Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000017Read_Master_Log_Pos: 399Relay_Log_File: mariadb-relay-bin.000002Relay_Log_Pos: 683Relay_Master_Log_File: mysql-bin.000017Slave_IO_Running: Yes //主从复制的线程IOSlave_SQL_Running: Yes //主从复制的线程SQLReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 399Relay_Log_Space: 979Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1031 row in set (0.00 sec)ERROR: No query specified

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。