#主数据库节点[root@nfs-server ~]# hostnamectl set-hostname mysql1[root@nfs-server ~]# logout[root@mysql1 ~]# [root@mysql1 ~]# setenforce 0[root@mysql1 ~]# systemctl stop firewalld[root@mysql1 ~]#[root@mysql1 ~]# vi /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.100.15 mysql1192.168.100.16 mysql2#安装数据库服务[root@mysql1 ~]# yum install -y mariadb mariadb-server[root@mysql1 ~]# systemctl start mariadb[root@mysql1 ~]# systemctl enable mariadb#初始化数据库 [root@mysql1 ~]# 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 currentpassword for the root user. If you've just installed MariaDB, andyou 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): # 默认按回车键ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)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 MariaDBroot user without the proper authorisation.Set root password? [Y/n] y #设置数据库root用户登录**New password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y #是否清除匿名用户? ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n #是否允许root用户是远程登录? ... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] y #是否删除test数据库? - Dropping test database... ... Success! - Removing privileges on test database... ... Success!Reloading the privilege tables will ensure that all changes made so farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB!#配置mysql1主节点[root@mysql1 ~]# vi /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socksymbolic-links=0#添加下面内容log_bin=mysql-binbinlog_ignore_db=mysqlserver_id=15[root@mysql1 ~]# systemctl restart mariadb[root@mysql1 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 2Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> #在mysql1节点中授权在任何客户端机器上可以以root用户登录到数据库;#然后在主节点上创建一个user用户连接节点mysql2,并赋予从节点同步主节点数据库的权限MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> grant replication slave on *.* to 'user'@'mysql2' identified by '000000';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> create database test; #主节点创建数据库Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> use test;Database changedMariaDB [test]> create table company(id int not null primary key,name varchar(50),addr varchar(255));Query OK, 0 rows affected (0.01 sec)MariaDB [test]> insert into company values(1,"alibaba","china"); Query OK, 1 row affected (0.01 sec)MariaDB [test]> select * from company;+----+---------+-------+| id | name | addr |+----+---------+-------+| 1 | alibaba | china |+----+---------+-------+#配置从数据库节点[root@nfs-server ~]# hostnamectl set-hostname mysql2[root@nfs-server ~]# lougout[root@mysql2 ~]# [root@mysql2 ~]# setenforce 0[root@mysql2 ~]# systemctl stop firewalld[root@mysql2 ~]# vi /etc/hosts::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.100.15 mysql1192.168.100.16 mysql2[root@mysql2 ~]# yum install -y mariadb mariadb-server[root@mysql2 ~]# systemctl start mariadb[root@mysql2 ~]# systemctl enable mariadb [root@mysql2 ~]# 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 currentpassword for the root user. If you've just installed MariaDB, andyou 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 MariaDBroot user without the proper authorisation.Set root password? [Y/n] yNew password: Re-enter new password: Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem. This is intended only for testing, and to make the installationgo a bit smoother. You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] y ... Success!Normally, root should only be allowed to connect from 'localhost'. Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] n ... skipping.By default, MariaDB comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB![root@mysql2 ~]# vi /etc/my.cnf #配置数据库主节点log_bin=mysql-bin #记录操作日志binlog_ignore_db=mysql #不同步系统数据库server_id=16 #节点id[root@mysql2 ~]# systemctl restart mariadb[root@mysql2 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 2Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> #从节点mysql2上登录mariadb数据库,配置从节点连接主机节点的连接信息MariaDB [(none)]> change master to master_host='mysql1',master_user='user',master_password='000000';Query OK, 0 rows affected (0.02 sec)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: mysql1 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 529 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 813 Relay_Master_Log_File: mysql-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: 529 Relay_Log_Space: 1109 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: 0Master_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: 151 row in set (0.00 sec)MariaDB [(none)]> show databases; #从节点验证复制功能+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec)MariaDB [(none)]> use test;MariaDB [test]> show tables;+----------------+| Tables_in_test |+----------------+| company |+----------------+MariaDB [test]> select * from company;+----+---------+-------+| id | name | addr |+----+---------+-------+| 1 | alibaba | china |+----+---------+-------+