服务器准备
主服务器:192.168.93.103
从服务器:192.168.93.102
主服务器操作
修改配置文件
[[email protected] ~]#vim /etc/my.cnf
[mysqld]
server_id=103 //指定一个服务id,如果不写这里默认为1
log_bin=/data/mysql/bin/mysql-bin //必须启动二进制日志
binlog_format=row //建议使用行row记录日志
innodb_file_per_table
datadir=/var/lib/mysql
创建主从复制用户
MariaDB [(none)]> grant replication slave on *.* to [email protected]‘192.168.93.%‘identified by ‘centos‘;
查看主服务器的二进制日志,记录要复制的位置
[[email protected] ~]#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主服务器已经运行了一段时间,主服务器的数据量非常大,而主从复制时间会很长,对主服务器压力过大
解决办法:先在主服务器上创建完全备份,还原到从服务器上后,再做主从复制
完全备份数据库
[[email protected] ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql
查看记录二进制日志中要复制的位置信息
[[email protected] ~]#vim /data/backup/all.sql
CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000018‘, MASTER_LOG_POS=245;
在从服务器需要做的操作:
修改配置文件
将主服务器完全备份的数据库文件all.sql拷贝至从服务器
[[email protected] ~]#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.sql
CHANGE MASTER TO
MASTER_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文件
[[email protected] ~]# mysql < all.sql
启动主从同步线程
MariaDB [(none)]> start slave;
从服务器操作
修改配置文件
[[email protected] ~]# vim /etc/my.cnf
[mysqld]
server_id=102 //指定从服务器的服务id,不能不写,默认为1,此id不能与其他服务器冲突
read_only=ON //设置数据库只读
使用有复制权限的用户账号连接至主服务器,配置同步信息
MariaDB [(none)]> CHANGE MASTER TO
MASTER_HOST=‘192.168.93.103‘, //远程主服务器IP
MASTER_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 event
Master_Host: 192.168.93.103
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000017
Read_Master_Log_Pos: 399
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 683
Relay_Master_Log_File: mysql-bin.000017
Slave_IO_Running: Yes //主从复制的线程IO
Slave_SQL_Running: Yes //主从复制的线程SQL
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: 399
Relay_Log_Space: 979
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: 103
1 row in set (0.00 sec)
ERROR: No query specified
原文地址:https://blog.51cto.com/14233815/2390068
时间: 2024-11-13 08:50:52