双节点环境
主服务器节点:192.168.200.4 node-1 从服务器节点:192.168.200.5 node-2 数据库版本:10.1.30
@H_404_5@安装mariadb
[root@node-1 ~]# yum install -y mariadb-server [root@node-2 ~]# yum install -y mariadb-server
@H_404_5@主服务器配置
[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
@H_404_5@从服务器配置
[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
@H_404_5@验证
#主服务器 [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)
@H_404_5@从服务器验证
[root@node-2 ~]# MysqL -uroot -p000000 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | @R_890_4045@ion_schema | | MysqL | | performance_schema | | test1 | +--------------------+ MariaDB [(none)]> use test1; Reading table @R_890_4045@ion 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)
@H_404_5@可以看到从服务器更新了主服务器的数据。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。