mysql-02 mysql-主从复制实验

mysql-主从复制实验

实验准备

node3:192.168.141.12 (master)

node4:192.168.141.132 (backup)

1、在node3、node4安装mariadb

1
2
3
4
5
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]# firewall-cmd --add-port=3306/tcp --permanent
[root@localhost ~]# firewall-cmd --reload

2、在node3、node4初始化mariadb数据库服务

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
52
53
54
55
56
57
58
59
60
61
62
[root@localhost ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
... Success!

Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
... Success!

Cleaning up...

All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[root@localhost ~]#

3、node3 编辑配置文件

1
2
3
4
5
6
7
8
9
[root@node3 ~]# vim /etc/my.cnf

#找到[mysqld]段,在其中加入下面两行代码

server-id=1
log-bin=mysql-bin

[root@node3 ~]# systemctl restart mariadb
[root@node3 ~]#

4、在node3上创建用户”tongbu”,设置其密码为123456,查看bin-log文件以及position点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@node3 ~]# mysql -u root -p
Enter password:

MariaDB [(none)]> grant replication slave on *.* to 'tongbu'@'%' identified by '123456';
MariaDB [(none)]> show master status;


+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 633 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>

如下图,记录下bin-log文件名(mysql-bin.000001)以及position(633)

5、配置node4(slave节点)

slave节点无需开启bin-log功能,但是其server-id需要和master不同。

1
2
3
4
5
6
7
[root@node4 ~]# vim /etc/my.cnf

#找到[mysqld]段,在其中加入下面1行代码

server-id=2

[root@node4 ~]# systemctl restart mariadb

6、slave指定master的IP(master的IP不要写错)、用户名、密码、bin-log文件名(mysql-bin.000001)以及position(633)

1
2
3
4
5
6
[root@node4 ~]# mysql -u root -p
Enter password:
MariaDB [(none)]> change master to master_host = '192.168.141.12',master_user='tongbu',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=633;
MariaDB [(none)]>
MariaDB [(none)]> slave start;
MariaDB [(none)]> show slave status\G

如下图,查看到如下两个yes,代表slave已经正常连接到了master实现了同步。

1
2
Slave_IO_Running: Yes   #I/O线程状态yes
Slave_SQL_Running: Yes #SQL线程状态yes

扩展知识

7、在node3(master)端创建数据库test和t表

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
MariaDB [mysql]> create database test charset=utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [mysql]> use test;
Database changed

MariaDB [test]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [test]>

MariaDB [test]> create table t (id varchar(20),name varchar(20));
Query OK, 0 rows affected (0.03 sec)

MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]>

8、在node4(slave)查看数据是否同步过来了

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
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test;
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
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| t |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]>

如上,slave从master复制数据成功,mysql主从架构至此配置成功。

9、测试

1)在node3(master)上插入两条数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
MariaDB [test]> insert into t values ("01","abcd");
Query OK, 1 row affected (0.01 sec)

MariaDB [test]> insert into t values ("02","efgh");
Query OK, 1 row affected (0.00 sec)

MariaDB [test]> select * from t;
+------+------+
| id | name |
+------+------+
| 01 | abcd |
| 02 | efgh |
+------+------+
2 rows in set (0.00 sec)

MariaDB [test]>

2)在node4(slave)上查看数据是否同步

1
2
3
4
5
6
7
8
9
10
MariaDB [test]> select * from t;
+------+------+
| id | name |
+------+------+
| 01 | abcd |
| 02 | efgh |
+------+------+
2 rows in set (0.00 sec)

MariaDB [test]>

至此, 验证结束,mysql主从复制实验成功完成。

欢迎打赏,谢谢
------ 本文结束------
0%