赞
踩
MySQL 是一款开源的 [ 关系型数据库软件系统 , RDBMS ] ,在GPL(General Public License) 的许可下根据需求可自定义源码。
数据库(database,DB):数据库是长期存在计算机内、有组织的、可共享的大量数据的集合。数据库中的数据按一定的数据模型组织、描述和存储,具有较小的冗余度(redundancy)、较高的数据独立性(data independency)和易扩展性(scalability),并可为各种用户共享。
在实际的生产中,为了解决Mysql的单点故障已经提高MySQL的整体服务性能,一般都会采用「主从复制」。主从复制中分为主服务器(master)和从服务器(slave),主服务器负责写,而从服务器负责读,Mysql的主从复制的过程是一个「异步的过程」。从数据库就是主数据库的备份。
互为主从的架构是指两台机器自己都是主机,并且也都是作为对方的从机。
分别是master和slave,master作为主数据库,slave作为从数据库。
- [root@localhost ~]# hostnamectl set-hostname master
- [root@localhost ~]# bash
- [root@localhost ~]# hostnamectl set-hostname slave
- [root@localhost ~]# bash
- 删除yum.repos.d下面所有的.repo包
- [root@master ~]# rm -rf /etc/yum.repos.d/*
- [root@master ~]# mkdir /opt/centos
- /dev/cdrom代表光驱
- [root@master ~]# mount /dev/cdrom /opt/centos/
- mount: /dev/sr0 写保护,将以只读方式挂载
-
- [root@master ~]# vi /etc/yum.repos.d/local.repo
- [root@master ~]# cat /etc/yum.repos.d/local.repo
- [centos]
- name=centos
- baseurl=file:///opt/centos
- enabled=1
- gpgcheck=0
- 使用yum clean all && yum repolist获取yum源信息
- [root@master ~]# yum clean all && yum repolist
- 已加载插件:fastestmirror
- 正在清理软件源: centos
- 已加载插件:fastestmirror
- Determining fastest mirrors
- centos | 3.6 kB 00:00:00
- (1/2): centos/group_gz | 153 kB 00:00:00
- (2/2): centos/primary_db | 3.3 MB 00:00:00
- 源标识 源名称 状态
- centos centos 4,070
- repolist: 4,070
-

master添加:
192.168.200.10 master
192.168.200.20 slave
- [root@master ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.200.10 master
- 192.168.200.20 slave
slave添加:
192.168.200.10 master
192.168.200.20 slave
- [root@slave ~]# vi /etc/hosts
- [root@slave ~]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.200.10 master
- 192.168.200.20 slave
两台虚拟机都要关闭防火墙
- #临时关闭selinux
- [root@master ~]# setenforce 0
-
- #禁用selinux(永久关闭)
- [root@master ~]# sed -i 's/SELINUX=enforcing/ SELINUX=disabled/g' /etc/selinux/config
-
- #临时关闭防火墙
- [root@master ~]# systemctl stop firewalld
-
- #关闭防火墙开机自启动,重启后生效
- [root@master ~]# systemctl disable firewalld
- Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
- Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
-
- #查看selinux状态
- [root@master ~]# getenforce
- Permissive
-
- #查看防火墙状态
- [root@master ~]# systemctl status firewalld
- ● firewalld.service - firewalld - dynamic firewall daemon
- Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
- Active: inactive (dead)
- Docs: man:firewalld(1)
-
- 12月 11 11:36:09 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
- 12月 11 11:36:09 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
- 12月 11 11:36:09 localhost.localdomain firewalld[736]: WARNING: AllowZoneDrifting is enabled....w.
- 12月 28 10:33:42 master systemd[1]: Stopping firewalld - dynamic firewall daemon...
- 12月 28 10:33:44 master systemd[1]: Stopped firewalld - dynamic firewall daemon.
- Hint: Some lines were ellipsized, use -l to show in full.

两台虚拟机都要安装MySQL数据库
[root@master ~]# yum -y install mariadb-server mariadb
两台虚拟机,启动数据库,并设置开机自启
- [root@master ~]# systemctl start mariadb
- [root@master ~]# systemctl enable mariadb
- Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
两台虚拟机,对数据库进行初始化操作
- [root@master ~]# 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: #输入数据库root密码000000
- Re-enter new password: #再次输入密码000000
- 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] n #同意root管理员从远程登录
- ... skipping.
-
- 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 #删除test数据库并取消对它的访问权限
- - 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!

在[mysqld]添加三行内容:
log_bin=mysql-bin #记录操作日志
binlog_ignore_db=mysql #不同步mysql系统数据库
server_id=10 #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字。例如:192.168.200.10,server_id就写10
- [root@master ~]# vi /etc/my.cnf
- [mysqld]
- log_bin=mysql-bin
- binlog_ignore_db=mysql
- server_id=10
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-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.log
- pid-file=/var/run/mariadb/mariadb.pid
-
- #
- # include all files from the config directory
- #
- !includedir /etc/my.cnf.d

- [root@master ~]# systemctl restart mariadb
- [root@master ~]# mysql -uroot -p000000
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 2
- Server version: 5.5.68-MariaDB MariaDB Server
-
- Copyright (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)]>
在master节点,授权在任何客户端机器上可以以root用户登录到数据库。
- MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
- Query OK, 0 rows affected (0.00 sec)
在master节点创建一个user用户,连接slave节点,赋予slave节点同步master节点数据库的权限。
- MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by "000000";
- Query OK, 0 rows affected (0.00 sec)
- [root@slave ~]# vi /etc/my.cnf
- [mysqld]
- log_bin=mysql-bin
- binlog_ignore_db=mysql
- server_id=20
- datadir=/var/lib/mysql
- socket=/var/lib/mysql/mysql.sock
- # Disabling symbolic-links is recommended to prevent assorted security risks
- symbolic-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.log
- pid-file=/var/run/mariadb/mariadb.pid
-
- #
- # include all files from the config directory
- #
- !includedir /etc/my.cnf.d

- [root@slave ~]# systemctl restart mariadb
- [root@slave ~]# mysql -uroot -p000000
- Welcome to the MariaDB monitor. Commands end with ; or \g.
- Your MariaDB connection id is 2
- Server version: 5.5.68-MariaDB MariaDB Server
-
- Copyright (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)]>
master_host为主节点名master,master_user是master创建的user用户
- MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='000000';
- Query OK, 0 rows affected (0.02 sec)
使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。
- 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 event
- Master_Host: master
- 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: 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: 10
- 1 row in set (0.00 sec)
-
- ERROR: No query specified
-

看到双yes,代表配置数据库主从成功。
需要把主从节点配置好,服务不能报错
在slave节点创建一个user用户连接master节点,master节点相互同步数据库的权限
- MariaDB [(none)]> grant replication slave on *.* to 'user'@'master' identified by '000000';
- Query OK, 0 rows affected (0.00 sec)
slave节点连接master节点的连接信息,master_host是master节点主机名slave,
- MariaDB [(none)]> change master to master_host='slave',master_user='user',master_password='000000';
- Query OK, 0 rows affected (0.03 sec)
- 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 event
- Master_Host: slave
- Master_User: user
- Master_Port: 3306
- Connect_Retry: 60
- Master_Log_File: mysql-bin.000002
- Read_Master_Log_Pos: 392
- Relay_Log_File: mariadb-relay-bin.000003
- Relay_Log_Pos: 676
- Relay_Master_Log_File: mysql-bin.000002
- 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: 392
- Relay_Log_Space: 1256
- 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: 20
- 1 row in set (0.00 sec)
-
- ERROR: No query specified

master节点服务状态为yes,数据库互为主从配置成功
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。