基础准备
| 主机名 | 镜像 | IP |
|---|---|---|
| MySQL1 | CentOS-7-x86_64-DVD-1511.iso | 192.168.200.10 |
| MySQL2 | CentOS-7-x86_64-DVD-1511.iso | 192.168.200.20 |
基础环境
MySQL节点1:[root@mysql ~]# hostnamectl set-hostname mysql1[root@mysql ~]# bash[root@mysql1 ~]# hostnamectlStatic hostname: mysql1Icon name: computer-vmChassis: vmMachine ID: eefd7b411d7347bdac743e0d446ba0b4Boot ID: 0ea3f72a474f4542bbf3be2ef30ff74cVirtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7Kernel: Linux 3.10.0-327.el7.x86_64Architecture: x86-64[root@mysql1 ~]# setenforce 0[root@mysql1 ~]# systemctl stop firewalld[root@mysql1 ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.200.10 mysql1192.168.200.20 mysql2[root@mysql1 ~]# yum install -y mariadb mariadb-server[root@mysql1 ~]# systemctl start mariadb[root@mysql1 ~]# systemctl enable mariadbCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.MySQL节点2:[root@localhost ~]# hostnamectl set-hostname mysql2[root@localhost ~]# bash[root@mysql2 ~]# hostnamectlStatic hostname: mysql2Icon name: computer-vmChassis: vmMachine ID: eefd7b411d7347bdac743e0d446ba0b4Boot ID: eded95cc080048098ffcd98d284c5edeVirtualization: vmwareOperating System: CentOS Linux 7 (Core)CPE OS Name: cpe:/o:centos:centos:7[root@mysql2 ~]# setenforce 0[root@mysql2 ~]# systemctl stop firewalld[root@mysql2 ~]# vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.200.10 mysql1192.168.200.20 mysql2Kernel: Linux 3.10.0-327.el7.x86_64Architecture: x86-64[root@mysql2 ~]# yum install -y mariadb mariadb-server[root@mysql2 ~]# systemctl start mariadb[root@mysql2 ~]# systemctl enable mariadbCreated symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
基础环境安装
MySQL节点1:[root@mysql1 ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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@mysql1 ~]# vim /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog_bin=mysql-bin \\开启MySQL binlog功能启用二进制日志binlog_ignore_db=mysql \\此参数表示不记录指定的数据库的二进制日志。server_id=10 \\服务器唯一IDdatadir=/var/lib/mysql \\MySQL默认的数据文档存储目录为/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock \\启动时指定socket文件的路径# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d[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)]> 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)]>MySQL节点2:与节点1一样MySQL先初始化再改配置[root@mysql2 ~]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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 ~]# vim /etc/my.cnf[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.socklog_bin = mysql-binbinlog_ignore_db = mysqlserver_id = 20datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0# Settings user and group are ignored when systemd is used.# If you need to run mysqld under a different user or group,# customize your systemd unit file for mariadb according to the# instructions in http://fedoraproject.org/wiki/Systemd[mysqld_safe]log-error=/var/log/mariadb/mariadb.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d[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)]> change master to-> master_host='mysql1',master_user='user',master_password='000000';Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]>MariaDB [(none)]> start slave;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> show slave status\G*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: mysql1Master_User: userMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 529Relay_Log_File: mariadb-relay-bin.000002Relay_Log_Pos: 813Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 529Relay_Log_Space: 1109Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id: 101 row in set (0.00 sec)
3.验证数据库主从服务
1)主节点创建数据库
先在主节点 mysql1 中创建库 bai,并在库 bai 中创建表 bai命令如下:
节点1:[root@mysql1 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 4Server 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)]> create database bai;Query OK, 1 row affected (0.00 sec)MariaDB [(none)]>MariaDB [(none)]> use bai;Database changedMariaDB [bai]> create table bai(id int not null primary key,name varchar(50),addr varchar(255));
2)从节点验证复制功能
登录 mysql2 节点的数据库,查看数据库列表。找到 bai 数据库,查询表.
节点2:[root@mysql2 ~]# mysql -uroot -p000000Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 5Server 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)]> show databases;+--------------------+| Database |+--------------------+| information_schema || bai || mysql || performance_schema |+--------------------+4 rows in set (0.01 sec)MariaDB [(none)]> use bai;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedMariaDB [bai]> show tables;+---------------+| Tables_in_bai |+---------------+| bai || company |+---------------+2 rows in set (0.00 sec)
