当前位置:   article > 正文

MySQL部署主从数据库,互为主从

主从数据库

(一)、数据库概念

1.什么是MySQL?

MySQL 是一款开源的 [ 关系型数据库软件系统 , RDBMS ] ,在GPL(General Public License) 的许可下根据需求可自定义源码。

2.什么是数据库?

数据库(database,DB):数据库是长期存在计算机内、有组织的、可共享的大量数据的集合。数据库中的数据按一定的数据模型组织、描述和存储,具有较小的冗余度(redundancy)、较高的数据独立性(data independency)和易扩展性(scalability),并可为各种用户共享。

3.什么是主从数据库?

在实际的生产中,为了解决Mysql的单点故障已经提高MySQL的整体服务性能,一般都会采用「主从复制」。主从复制中分为主服务器(master)和从服务器(slave),主服务器负责写,而从服务器负责读,Mysql的主从复制的过程是一个「异步的过程」。从数据库就是主数据库的备份。

4.什么是互为主从数据库?

互为主从的架构是指两台机器自己都是主机,并且也都是作为对方的从机。


(二)、部署数据库基础操作环境

1.配置基础环境

1.1更改虚拟机的主机名

分别是master和slave,master作为主数据库slave作为从数据库

  1. [root@localhost ~]# hostnamectl set-hostname master
  2. [root@localhost ~]# bash
  1. [root@localhost ~]# hostnamectl set-hostname slave
  2. [root@localhost ~]# bash

1.2分别在两台虚拟机配置本地yum源

  1. 删除yum.repos.d下面所有的.repo包
  2. [root@master ~]# rm -rf /etc/yum.repos.d/*
  3. [root@master ~]# mkdir /opt/centos
  4. /dev/cdrom代表光驱
  5. [root@master ~]# mount /dev/cdrom /opt/centos/
  6. mount: /dev/sr0 写保护,将以只读方式挂载
  7. [root@master ~]# vi /etc/yum.repos.d/local.repo
  8. [root@master ~]# cat /etc/yum.repos.d/local.repo
  9. [centos]
  10. name=centos
  11. baseurl=file:///opt/centos
  12. enabled=1
  13. gpgcheck=0
  14. 使用yum clean all && yum repolist获取yum源信息
  15. [root@master ~]# yum clean all && yum repolist
  16. 已加载插件:fastestmirror
  17. 正在清理软件源: centos
  18. 已加载插件:fastestmirror
  19. Determining fastest mirrors
  20. centos | 3.6 kB 00:00:00
  21. (1/2): centos/group_gz | 153 kB 00:00:00
  22. (2/2): centos/primary_db | 3.3 MB 00:00:00
  23. 源标识 源名称 状态
  24. centos centos 4,070
  25. repolist: 4,070

1.3配置hosts文件解析

master添加:

192.168.200.10 master
192.168.200.20 slave

  1. [root@master ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 192.168.200.10 master
  5. 192.168.200.20 slave

slave添加:

192.168.200.10 master
192.168.200.20 slave

  1. [root@slave ~]# vi /etc/hosts
  2. [root@slave ~]# cat /etc/hosts
  3. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  4. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  5. 192.168.200.10 master
  6. 192.168.200.20 slave

1.4关闭防火墙

两台虚拟机都要关闭防火墙

  1. #临时关闭selinux
  2. [root@master ~]# setenforce 0
  3. #禁用selinux(永久关闭)
  4. [root@master ~]# sed -i 's/SELINUX=enforcing/ SELINUX=disabled/g' /etc/selinux/config
  5. #临时关闭防火墙
  6. [root@master ~]# systemctl stop firewalld
  7. #关闭防火墙开机自启动,重启后生效
  8. [root@master ~]# systemctl disable firewalld
  9. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
  10. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  11. #查看selinux状态
  12. [root@master ~]# getenforce
  13. Permissive
  14. #查看防火墙状态
  15. [root@master ~]# systemctl status firewalld
  16. ● firewalld.service - firewalld - dynamic firewall daemon
  17. Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
  18. Active: inactive (dead)
  19. Docs: man:firewalld(1)
  20. 1211 11:36:09 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
  21. 1211 11:36:09 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
  22. 1211 11:36:09 localhost.localdomain firewalld[736]: WARNING: AllowZoneDrifting is enabled....w.
  23. 1228 10:33:42 master systemd[1]: Stopping firewalld - dynamic firewall daemon...
  24. 1228 10:33:44 master systemd[1]: Stopped firewalld - dynamic firewall daemon.
  25. Hint: Some lines were ellipsized, use -l to show in full.

2.安装MySQL数据库

2.1使用yum源安装

两台虚拟机都要安装MySQL数据库

[root@master ~]# yum -y install mariadb-server mariadb

2.2启动数据库

两台虚拟机,启动数据库,并设置开机自启

  1. [root@master ~]# systemctl start mariadb
  2. [root@master ~]# systemctl enable mariadb
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

2.3初始操作

两台虚拟机,对数据库进行初始化操作

  1. [root@master ~]# mysql_secure_installation
  2. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  3. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  4. In order to log into MariaDB to secure it, we'll need the current
  5. password for the root user. If you've just installed MariaDB, and
  6. you haven't set the root password yet, the password will be blank,
  7. so you should just press enter here.
  8. Enter current password for root (enter for none): #当前数据库密码为空,直接按回车键
  9. OK, successfully used password, moving on...
  10. Setting the root password ensures that nobody can log into the MariaDB
  11. root user without the proper authorisation.
  12. Set root password? [Y/n] y
  13. New password: #输入数据库root密码000000
  14. Re-enter new password: #再次输入密码000000
  15. Password updated successfully!
  16. Reloading privilege tables..
  17. ... Success!
  18. By default, a MariaDB installation has an anonymous user, allowing anyone
  19. to log into MariaDB without having to have a user account created for
  20. them. This is intended only for testing, and to make the installation
  21. go a bit smoother. You should remove them before moving into a
  22. production environment.
  23. Remove anonymous users? [Y/n] y #删除匿名账户
  24. ... Success!
  25. Normally, root should only be allowed to connect from 'localhost'. This
  26. ensures that someone cannot guess at the root password from the network.
  27. Disallow root login remotely? [Y/n] n #同意root管理员从远程登录
  28. ... skipping.
  29. By default, MariaDB comes with a database named 'test' that anyone can
  30. access. This is also intended only for testing, and should be removed
  31. before moving into a production environment.
  32. Remove test database and access to it? [Y/n] y #删除test数据库并取消对它的访问权限
  33. - Dropping test database...
  34. ... Success!
  35. - Removing privileges on test database...
  36. ... Success!
  37. Reloading the privilege tables will ensure that all changes made so far
  38. will take effect immediately.
  39. Reload privilege tables now? [Y/n] y #刷新授权表,让初始化后的设定立即生效
  40. ... Success!
  41. Cleaning up...
  42. All done! If you've completed all of the above steps, your MariaDB
  43. installation should now be secure.
  44. Thanks for using MariaDB!

(三)、配置主从数据库

1.配置master主数据库

1.1修改主数据库的配置文件

在[mysqld]添加三行内容:

log_bin=mysql-bin         #记录操作日志
binlog_ignore_db=mysql    #不同步mysql系统数据库
server_id=10           #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字。例如:192.168.200.10,server_id就写10

  1. [root@master ~]# vi /etc/my.cnf
  2. [mysqld]
  3. log_bin=mysql-bin
  4. binlog_ignore_db=mysql
  5. server_id=10
  6. datadir=/var/lib/mysql
  7. socket=/var/lib/mysql/mysql.sock
  8. # Disabling symbolic-links is recommended to prevent assorted security risks
  9. symbolic-links=0
  10. # Settings user and group are ignored when systemd is used.
  11. # If you need to run mysqld under a different user or group,
  12. # customize your systemd unit file for mariadb according to the
  13. # instructions in http://fedoraproject.org/wiki/Systemd
  14. [mysqld_safe]
  15. log-error=/var/log/mariadb/mariadb.log
  16. pid-file=/var/run/mariadb/mariadb.pid
  17. #
  18. # include all files from the config directory
  19. #
  20. !includedir /etc/my.cnf.d

1.2重启数据库服务,进入数据库

  1. [root@master ~]# systemctl restart mariadb
  2. [root@master ~]# mysql -uroot -p000000
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 2
  5. Server version: 5.5.68-MariaDB MariaDB Server
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MariaDB [(none)]>

1.3在主数据库授权

在master节点,授权在任何客户端机器上可以以root用户登录到数据库。

  1. MariaDB [(none)]> grant all privileges on *.* to root@'%' identified by "000000";
  2. Query OK, 0 rows affected (0.00 sec)

 在master节点创建一个user用户,连接slave节点,赋予slave节点同步master节点数据库的权限。

  1. MariaDB [(none)]> grant replication slave on *.* to 'user'@'slave' identified by "000000";
  2. Query OK, 0 rows affected (0.00 sec)

2.配置slave从数据库

2.1修改从数据库的配置文件

  1. [root@slave ~]# vi /etc/my.cnf
  2. [mysqld]
  3. log_bin=mysql-bin
  4. binlog_ignore_db=mysql
  5. server_id=20
  6. datadir=/var/lib/mysql
  7. socket=/var/lib/mysql/mysql.sock
  8. # Disabling symbolic-links is recommended to prevent assorted security risks
  9. symbolic-links=0
  10. # Settings user and group are ignored when systemd is used.
  11. # If you need to run mysqld under a different user or group,
  12. # customize your systemd unit file for mariadb according to the
  13. # instructions in http://fedoraproject.org/wiki/Systemd
  14. [mysqld_safe]
  15. log-error=/var/log/mariadb/mariadb.log
  16. pid-file=/var/run/mariadb/mariadb.pid
  17. #
  18. # include all files from the config directory
  19. #
  20. !includedir /etc/my.cnf.d

2.2重启数据库服务,进入数据库

  1. [root@slave ~]# systemctl restart mariadb
  2. [root@slave ~]# mysql -uroot -p000000
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 2
  5. Server version: 5.5.68-MariaDB MariaDB Server
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MariaDB [(none)]>

2.3slave节点连接master信息

master_host为主节点名master,master_user是master创建的user用户

  1. MariaDB [(none)]> change master to master_host='master',master_user='user',master_password='000000';
  2. Query OK, 0 rows affected (0.02 sec)

2.4查看slave从节点服务状态

使用show slave status\G命令,并查看从节点服务状态,如果Slave_IO_Running和Slave_SQL_Running的状态都为YES,则从节点服务开启成功。

  1. MariaDB [(none)]> start slave;
  2. Query OK, 0 rows affected (0.00 sec)
  3. MariaDB [(none)]> show slave status\G;
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: master
  7. Master_User: user
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000001
  11. Read_Master_Log_Pos: 529
  12. Relay_Log_File: mariadb-relay-bin.000002
  13. Relay_Log_Pos: 813
  14. Relay_Master_Log_File: mysql-bin.000001
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
  23. Last_Errno: 0
  24. Last_Error:
  25. Skip_Counter: 0
  26. Exec_Master_Log_Pos: 529
  27. Relay_Log_Space: 1109
  28. Until_Condition: None
  29. Until_Log_File:
  30. Until_Log_Pos: 0
  31. Master_SSL_Allowed: No
  32. Master_SSL_CA_File:
  33. Master_SSL_CA_Path:
  34. Master_SSL_Cert:
  35. Master_SSL_Cipher:
  36. Master_SSL_Key:
  37. Seconds_Behind_Master: 0
  38. Master_SSL_Verify_Server_Cert: No
  39. Last_IO_Errno: 0
  40. Last_IO_Error:
  41. Last_SQL_Errno: 0
  42. Last_SQL_Error:
  43. Replicate_Ignore_Server_Ids:
  44. Master_Server_Id: 10
  45. 1 row in set (0.00 sec)
  46. ERROR: No query specified

看到双yes,代表配置数据库主从成功。


(四)、配置互为主从数据库

需要把主从节点配置好,服务不能报错

1.slave节点

在slave节点创建一个user用户连接master节点,master节点相互同步数据库的权限

  1. MariaDB [(none)]> grant replication slave on *.* to 'user'@'master' identified by '000000';
  2. Query OK, 0 rows affected (0.00 sec)

2.master节点

slave节点连接master节点的连接信息,master_host是master节点主机名slave,

  1. MariaDB [(none)]> change master to master_host='slave',master_user='user',master_password='000000';
  2. Query OK, 0 rows affected (0.03 sec)

2.1查看master节点服务状态

  1. MariaDB [(none)]> start slave;
  2. Query OK, 0 rows affected (0.00 sec)
  3. MariaDB [(none)]> show slave status \G;
  4. *************************** 1. row ***************************
  5. Slave_IO_State: Waiting for master to send event
  6. Master_Host: slave
  7. Master_User: user
  8. Master_Port: 3306
  9. Connect_Retry: 60
  10. Master_Log_File: mysql-bin.000002
  11. Read_Master_Log_Pos: 392
  12. Relay_Log_File: mariadb-relay-bin.000003
  13. Relay_Log_Pos: 676
  14. Relay_Master_Log_File: mysql-bin.000002
  15. Slave_IO_Running: Yes
  16. Slave_SQL_Running: Yes
  17. Replicate_Do_DB:
  18. Replicate_Ignore_DB:
  19. Replicate_Do_Table:
  20. Replicate_Ignore_Table:
  21. Replicate_Wild_Do_Table:
  22. Replicate_Wild_Ignore_Table:
  23. Last_Errno: 0
  24. Last_Error:
  25. Skip_Counter: 0
  26. Exec_Master_Log_Pos: 392
  27. Relay_Log_Space: 1256
  28. Until_Condition: None
  29. Until_Log_File:
  30. Until_Log_Pos: 0
  31. Master_SSL_Allowed: No
  32. Master_SSL_CA_File:
  33. Master_SSL_CA_Path:
  34. Master_SSL_Cert:
  35. Master_SSL_Cipher:
  36. Master_SSL_Key:
  37. Seconds_Behind_Master: 0
  38. Master_SSL_Verify_Server_Cert: No
  39. Last_IO_Errno: 0
  40. Last_IO_Error:
  41. Last_SQL_Errno: 0
  42. Last_SQL_Error:
  43. Replicate_Ignore_Server_Ids:
  44. Master_Server_Id: 20
  45. 1 row in set (0.00 sec)
  46. ERROR: No query specified

master节点服务状态为yes,数据库互为主从配置成功

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/1002113
推荐阅读
相关标签
  

闽ICP备14008679号