怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下:
1、主从服务器分别作以下操作:
1.1、MYSQL版本一致
1.2、初始化表,并在后台启动mysql
1.3、主ip:139.0.0.31
,从IP:139.0.0.32
2、修改主服务器master:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin //[必须]启用二进制日志
server-id=31
//[必须]服务器唯一ID,默认是1,一般取IP最后一段
max_binlog_size= 104857600
//最大日志文件为100M
3、修改从服务器slave:
#vi /etc/my.cnf
[mysqld]
log-bin=mysql-bin
//[必须]启用二进制日志
server-id=32 //[必须]服务器唯一ID,默认是1,一般取IP最后一段
max_binlog_size= 104857600 //最大日志文件为100M
4、重启两台服务器的mysql
/etc/init.d/mysqld
restart
5、在主服务器上建立帐户并授权slave:
#mysql -uroot
-p密码
mysql>grant replication slave on *.* to 'mysync'@'%' identified by 'mysync';
//一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体客户端IP代替,如139.0.0.32,加强安全。
6、登录主服务器的mysql,查询master的状态
mysql>show
master status;
+------------------+----------+--------------+------------------+
| File
| Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
|
mysql-bin.000001 | 98 | | |
+------------------+----------+--------------+------------------+
1 row
in set (0.00 sec)
注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值Position变化,下一步会用到。
7、配置从服务器Slave:
mysql>change master to
master_host='139.0.0.31',master_user='mysync',master_password='mysync',master_log_file='mysql-bin.000001',master_log_pos=98;
//注意不要断开,“98”无单引号。
Mysql>start slave;
//启动从服务器复制功能
8、检查从服务器复制功能状态:
mysql> show slave
status;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
|
Slave_IO_State | Master_Host | Master_User | Master_Port |
Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File
| Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running |
Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB |
Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table |
Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter |
Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File |
Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path |
Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master
|
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
|
Waiting for master to send event | 139.0.0.31 | mysync | 3306 |
60 | mysql-bin.000001 | 98 | mysqld-relay-bin.000002 |
235 | mysql-bin.000001 | Yes
| Yes | |
| | |
| | 0 | |
0 | 98 | 235 | None | |
0 | No | | |
| | | 0 |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+
1
row in set (0.00 sec)
mysql> show slave
status\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 139.0.0.31
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File:
mysqld-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
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: 0
Relay_Log_Space: 117
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: NULL
1 row in set (0.00
sec)
注:Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。
以上操作过程,主从服务器配置完成。
9、主从服务器测试:
主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
mysql>
create database ucds;
Query OK, 1 row affected (0.00
sec)
mysql> use ucds;
Database
changed
mysql> create table t(a int,b int);
Query OK, 0 rows
affected (0.00 sec)
mysql> insert into t values(1,1);
Query OK,
1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ucds
|
| mysql |
| test |
+--------------------+
4 rows in set (0.00
sec)
从服务器Mysql查询:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ucds
| //I'M here,大家看到了吧
| mysql |
| test
|
+--------------------+
4 rows in set (0.00 sec)
mysql> use ucds
Database changed
mysql> select * from t;
//可以看到在主服务器上新增的具体数据
+------+------+
| a | b |
+------+------+
| 1 | 1 |
+------+------+
1 row in set
(0.00 sec)
10、完成:
用网管监控slave的两个“yes”,如发现只有一个或零个“yes”,就表明主从有问题了,警报。
11、后期日志清理:
随着日期的增长,MYSQL会产生大量的日志,占用大量的磁盘空间,主服务器 mysql-bin.00000X ,从服务器
mysqld-relay-bin.00000X
主服务器清理日志方式如下:
A:在每个从服务器上,使用SHOW SLAVE
STATUS来检查它正在读取哪个日志。
B:使用SHOW MASTER
LOGS获得主服务器上的一系列日志。
C:在所有的从服务器中判定最早的日志,这个是目标日志,如果所有的从属服务器是更新的,就是清单上的最后一个日志。
D:清理所有的日志,但是不包括目标日志,因为从服务器还要跟它同步。
清理日志方法为:
PURGE
MASTER LOGS TO 'mysql-bin.000010';
PURGE MASTER LOGS BEFORE '2012-12-30
21:00:00';
如果你确定从服务器已经同步过了,跟主服务器一样了,那么可以直接RESET
MASTER将这些文件删除。
mysql> reset master;
Query OK, 0 rows affected (0.02
sec)
在mysql里show了一下variables,
mysql> show variables like
'%log%';
查到了
| expire_logs_days | 0
|
这个默认是0,也就是logs不过期,这个是一个global的参数,所以需要执行
set global
expire_logs_days=7;
这样7天前的log就会被删除了,但这样设置还不行,下次重启mysql了,配置又恢复默认了,所以需在my.cnf中设置
expire_logs_days = 7
这样重启也不怕了
从服务器清理日志方式:
如果你确定从服务器已经同步过了,跟主服务器一样了,
那么可以先STOP
SLAVE,然后RESET SLAVE将这些文件删除。
mysql> stop slave;
Query OK, 0 rows
affected (0.01 sec)
mysql> reset
slave;
Query OK, 0 rows affected (0.15 sec)
清理完日志后,主从配置需重新设置。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/20553601/viewspace-1065551/
,如需转载,请注明出处,否则将追究法律责任。