双节点搭建mariadb主从数据库

双节点环境

主服务器节点:192.168.200.4 node-1
从服务器节点:192.168.200.5 node-2
数据库版本:10.1.30

安装mariadb

[root@node-1 ~]# yum install -y mariadb-server
[root@node-2 ~]# yum install -y mariadb-server

主服务器配置

[root@node-1 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
innodb_file_per_table=NO
log-bin=/var/lib/mysql/master-bin #log-bin没指定存储目录,则是默认datadir指向的目录
binlog_format=mixed
server-id=200 #每个服务器都需要添加server_id配置,各个服务器的server_id不能重复。
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#启动mariadb
[root@node-1 ~]# systemctl start mariadb

#设置mysql登陆密码
[root@node-1 ~]# mysql_secure_installation 
Enter current password for root (enter for none):  #按回车
Set root password? [Y/n] y
New password:  #设置密码
Re-enter new password:  #确认密码
Remove anonymous users? [Y/n] y #删除匿名用户
Disallow root login remotely? [Y/n] n #远程禁止root登陆
Remove test database and access to it? [Y/n] y #删除测试数据库并访问它
Reload privilege tables now? [Y/n] y #现在重新加载特权表

#登陆数据库
[root@node-1 ~]# mysql -uroot -p000000
#创建帐号并赋予replication的权限 
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.200.%' IDENTIFIED BY '000000';       
Query OK, 0 rows affected (0.00 sec)

#备份数据库数据,用于导入到从数据库中
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |     1502 |              |                  |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)  #记住file和position 从服务器要用到

[root@node-1 ~]# mysqldump -uroot -p000000 --all-databases > mysql.sql #导出数据库
[root@node-1 ~]# ls
mysql.sql
#将mysql.sql复制到从服务器上
[root@node-1 ~]# scp mysql.sql node-2:/root/
mysql.sql                                                              100%  466KB 466.4KB/s   00:00 

从服务器配置

[root@node-2 ~]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
innodb_file_per_table=NO
server-id=201
relay-log=/var/lib/mysql/relay-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#启动mariadb
[root@node-2 ~]# systemctl start mariadb
#设置登陆密码,和上面一样
[root@node-2 ~]# mysql_secure_installation 
#导入主服务器的数据库
[root@node-2 ~]# mysql -uroot -p000000 < mysql.sql 

#登陆数据库
[root@node-2 ~]# mysql -uroot -p000000
#设置主从复制
MariaDB [(none)]> change master to master_host='192.168.200.4',master_user='root',master_password='000000',master_log_file='master-bin.000001',master_log_pos=1502;
Query OK, 0 rows affected (0.18 sec) 
#这里的master_log_file,,master_log_pos就是刚才主服务器上面的file和position内容
#master_host设置当前服务器为主服务器(192.168.200.4)的从库

#开启主从复制
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
#查看从库状态
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.200.4
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1502
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 538
        Relay_Master_Log_File: master-bin.000001
             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: 1502
              Relay_Log_Space: 830
              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: 200
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
                   Using_Gtid: No
                  Gtid_IO_Pos: 
      Replicate_Do_Domain_Ids: 
  Replicate_Ignore_Domain_Ids: 
                Parallel_Mode: conservative
1 row in set (0.00 sec)

#结果中Slave_IO_Running和Slave_SQL_Running必须为Yes

验证

#主服务器
[root@node-1 ~]# mysql -uroot -p000000      
MariaDB [(none)]> create database test1;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test1;
Database changed
MariaDB [test1]> create table user (id int);  
Query OK, 0 rows affected (0.33 sec)

MariaDB [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)

MariaDB [test1]> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

从服务器验证

[root@node-2 ~]# mysql -uroot -p000000
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test1              |
+--------------------+
MariaDB [(none)]> use test1;
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 [test1]> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| user            |
+-----------------+
1 row in set (0.00 sec)

MariaDB [test1]> desc user;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

可以看到从服务器更新了主服务器的数据。

猜你喜欢

转载自blog.51cto.com/9103824/2384946