MySQL群集,主从复制及双主模式
时间:2016-07-23 16:41 来源:linux.it.net.cn 作者:IT
1
2
# mkdir /mydata
# mkdir /mylog
1
2
3
# scpmariadb-5.5.36-linux-x86_64.tar.gz 192.168.122.136:/root
root@192.168.122.136'spassword:
mariadb-5.5.36-linux-x86_64.tar.gz 100% 212MB 11.2MB/s 00:19
1
2
3
4
5
6
# tar xfmariadb-5.5.36-linux-x86_64.tar.gz -C /usr/local/
# useradd -r -s/sbin/nologin mysql
# chownmysql:mysql /my*
# ls -ld /my*
drwxr-xr-x 2 mysqlmysql 4096 Sep 20 14:35 /mydata
drwxr-xr-x 2 mysqlmysql 4096 Sep 20 14:36 /mylog
1
2
3
# ln -smariadb-5.5.36-linux-x86_64/ mysql
# mkdir /etc/mysql
# chown -Rmysql:mysql mysql/
1
2
# cpsupport-files/my-large.cnf /etc/mysql/my.cnf
# cpsupport-files/mysql.server /etc/init.d/mysqld
1
2
3
# vim/etc/mysql/my.cnf
datedir =/mydata/data
log-bin=/mylog/master-bin
1
2
3
# chmod +x/etc/init.d/mysqld
# chkconfig --addmysqld
#./scripts/mysql_install_db --user=mysql --datadir=/mydata/data
1
2
3
4
# service mysqld start
Starting MySQL..SUCCESS!
# ss -tnl
LISTEN 0 50 *:3306 *:
1
2
3
4
5
6
# ls /my*
/mydata:
data
/mylog:
master-bin.000001 master-bin.000002 master-bin.000003 master-bin.000004 master-bin.index
1
2
3
4
5
6
7
8
9
10
11
12
13
# mkdir /relaylog
# vim/etc/mysql/my.cnf
datadir =/mydata/data
#log-bin=mysql-bin
#binlog_format=mixed
server-id = 10
relay-log = /relaylog/relay-log
# service mysqldstart
Starting MySQL...SUCCESS!
# ss -tnl
LISTEN 0 50 *:3306 *:*
1
2
3
4
5
6
# mysql
MariaDB[(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TOubu@'192.168.122.136' IDENTIFIED BY '123456';
Query OK, 0 rowsaffected (0.00 sec)
MariaDB[(none)]> FLUSH PRIVILEGES;
Query OK, 0 rowsaffected (0.00 sec)
1
2
3
4
# mysql -u ubu -h192.168.122.134 -p
Enter password:
MariaDB[(none)]>
1
2
3
4
5
6
7
8
MariaDB[(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB |Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
|master-bin.000004 | 492 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.01sec)
因为是开始重启失败,重新初始化了一次,导致日志已经走到000004的位置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
MariaDB[(none)]> CHANGE MASTER TOMASTER_HOST='192.168.122.134',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-bin.000004',MASTER_LOG_POS=492;
Query OK, 0 rowsaffected (0.15 sec)
MariaDB[(none)]> START SLAVE;
MariaDB[(none)]> SHOW SLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.122.134
Master_User: ubu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:master-bin.000004
Read_Master_Log_Pos: 492
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 530
Relay_Master_Log_File:master-bin.000004
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: 492
Relay_Log_Space: 818
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: 1
1 row in set (0.00sec)
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
MariaDB [(none)]> CREATE DATABASEtestdb;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set (0.01 sec)
1
MariaDB [(none)]> SET GLOBALread_only=1;
1
2
MariaDB [(none)]> GRANTCREATE,UPDATE,INSERT ON testdb.* TO testuser@'192.168.%.%' IDENTIFIED BY'testuser';
Query OK, 0 rows affected (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
MariaDB [mysql]> SELECT User,Host FROMuser;
+----------+---------------+
| User | Host |
+----------+---------------+
| root | 127.0.0.1 |
| testuser | 192.168.%.% |
| root | ::1 |
| | localhost |
| root | localhost |
| | www.zhang.com |
| root | www.zhang.com |
+----------+---------------+
7 rows in set (0.00 sec)
1
2
3
4
# mysql -u testuser -h 192.168.122.136 -p
MariaDB [(none)]> CREATE DATABASEtestuser;
ERROR 1290 (HY000): The MariaDB server isrunning with the --read-only option so it cannot execute this statement
1
2
3
4
5
6
7
# vim/etc/mysql/my.cnf
datadir =/mydata/data
log-bin=/mylog/master-bin
relay-log=/myrelailogs/master-relay-log
server-id = 1
auto-increment-offset = 1
auto-increment-increment= 2
1
2
3
4
5
6
7
# vim/etc/mysql/my.cnf
datadir =/mydata/data
server-id = 10
relay-log = /relaylog/relay-log
log-bin =/mybinlog/master2-bin
auto-increment-offset = 2
auto-increment-increment= 2
1
MariaDB[(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TOubu@'192.168.%.%' IDENTIFIED BY '123456';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
MariaDB[(none)]> CHANGE MASTER TOMASTER_HOST='192.168.122.136',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master2-bin.000003',MASTER_LOG_POS=418;
Query OK, 0 rowsaffected (0.01 sec)
MariaDB[(none)]> START SLAVE;
Query OK, 0 rowsaffected (0.00 sec)
MariaDB[(none)]> SHOW SLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.122.136
Master_User: ubu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:master2-bin.000003
Read_Master_Log_Pos: 418
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 531
Relay_Master_Log_File:master2-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
MariaDB[(none)]> CHANGE MASTER TO MASTER_HOST='192.168.122.134',MASTER_USER='ubu',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master1-bin.000003',MASTER_LOG_POS=418;
Query OK, 0 rowsaffected (0.13 sec)
MariaDB[(none)]> START SLAVE;
Query OK, 0 rowsaffected (0.00 sec)
MariaDB[(none)]> SHOW SLAVE STATUS\G
***************************1. row ***************************
Slave_IO_State: Waiting formaster to send event
Master_Host: 192.168.122.134
Master_User: ubu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:master1-bin.000003
Read_Master_Log_Pos: 418
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 531
Relay_Master_Log_File:master1-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MariaDB[(none)]> CREATE DATABASE testdb;
Query OK, 1 rowaffected (0.01 sec)
B节点数据库库查看
MariaDB[(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
|performance_schema |
| test |
| testdb |
+--------------------+
5 rows in set(0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
MariaDB[(none)]> DROP DATABASE testdb;
Query OK, 0 rowsaffected (0.06 sec)
A节点查看
MariaDB[(none)]> CREATE DATABASE testdb;
Query OK, 1 rowaffected (0.01 sec)
MariaDB[(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
|information_schema |
| mysql |
|performance_schema |
| test |
+--------------------+
4 rows in set(0.01 sec)
(责任编辑:IT)
(责任编辑:IT) |
构建主从复制的几个事项: