部署主从数据库

规划节点

ip主机名节点
192.168.200.50mysql1主数据库节点
192.168.200.51mysql2从数据库节点

基础准备:centos:1511,1核\2G内存\20G硬盘

基础环境安装

修改主机名
使用远程工具xshell连接到192.168.200.50、192.168.200.51这两台虚拟机,并对这两台虚拟机进行修改主机名的操作,192.168.200.50主机名修改为mysql1,192.168.200.51主机名修改为mysql2。

# hostnamectl set-hostname mysql1
# hostnamectl set-hostname mysql2

关闭防火墙以及selinux服务
两个节点关闭防火墙firewalld及SELinux服务,命令如下:

# systemctl stop firewalld && systemctl disable firewalld
# setenforce 0

# cat /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled    #禁用selinux。
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

配置hosts文件
两个节点配置/etc/hosts文件,修改为如下:

# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.50 mysql1
192.168.200.51 mysql2

配置YUM源并安装数据库服务
挂载CentOS-7-x86_64-DVD-1511.iso镜像并自行配置本地YUM源,配置完毕后,两个节点安装数据库服务,命令如下:

# mv /etc/yum.repos.d/* /media/
# cd /opt/
# mkdir centos
# cd
# mount /dev/cdrom /opt/centos/
mount: /dev/sr0 is write-protected, mounting read-only
# cat /etc/yum.repos.d/local.repo 
[centos]
name=centos
baseurl=file:///opt/centos
gpgcheck=0
enabled=1
# yum clean all && yum list | grep mariadb
# yum install -y mariadb mariadb-server

两个节点启动数据库服务并设置开机自启,命令如下:

# systemctl start mariadb
# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

初始化数据库并配置主从服务

初始化数据库
两个节点初始化数据库,配置数据库root密码为000000,命令如下:

[root@mysql1 ~]# mysql_secure_installation 
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

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
 ... 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
 - 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!

配置mysql1主节点
修改mysql1节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容:

[root@mysql1 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 50                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.50,server_id就写50
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@mysql1 ~]# systemctl restart mariadb
[root@mysql1 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, 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)

配置mysql2从节点
修改mysql2节点的数据库配置文件,在配置文件/etc/my.cnf中的[mysqld]增添如下内容:

[root@mysql2 ~]# cat /etc/my.cnf
[mysqld]
log_bin = mysql-bin                       #记录操作日志
binlog_ignore_db = mysql                  #不同步mysql系统数据库
server_id = 51                            #数据库集群中的每个节点id都要不同,一般使用IP地址的最后段的数字,例如192.168.200.51,server_id就写51
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

在从节点mysql2上登录MariaDB数据库,配置从节点连接主节点的连接信息。master_host为主节点主机名mysql1,master_user为上一步中创建的用户user,命令如下:

[root@mysql2 ~]# systemctl restart mariadb
[root@mysql2 ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, 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.01 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: 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: 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: 50
1 row in set (0.00 sec)

可以看到Slave_IO_Running和Slave_SQL_Running的状态都是Yes,配置数据库主从集群成功。

验证数据库主从服务

主节点创建数据库
先在主节点mysql1中创建库test,并在库test中创建表company,插入表数据,创建完成后,查看表company数据,命令如下:

[root@mysql1 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use test;
Database changed
MariaDB [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.00 sec)

MariaDB [test]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

从节点验证复制功能
登录mysql2节点的数据库,查看数据库列表。找到test数据库,查询表,并查询内容验证从数据库的复制功能,命令如下:

[root@mysql2 ~]# mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.44-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, 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 |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| company        |
+----------------+
1 row in set (0.00 sec)

MariaDB [test]> select * from company;
+----+---------+-------+
| id | name    | addr  |
+----+---------+-------+
|  1 | alibaba | china |
+----+---------+-------+
1 row in set (0.00 sec)

可以查看到主数据库中刚刚创建的库、表、信息,验证从数据库的复制功能成功。

### 回答1: Linux部署主从数据库需要以下步骤: 1. 安装数据库软件,如MySQL或PostgreSQL。 2. 在主数据库上创建一个新的用户,并授予该用户复制权限。 3. 在主数据库上启用二进制日志功能,以便将更改记录到二进制日志文件中。 4. 在从数据库上创建一个新的数据库,并将其设置为从主数据库复制数据的目标。 5. 在从数据库上配置主数据库的连接信息,并启用从数据库的复制功能。 6. 测试主从数据库的连接和同步状态,确保数据能够正确地复制到从数据库中。 7. 定期监控主从数据库的状态,以确保它们始终保持同步并且没有出现故障。 总之,部署主从数据库需要一定的技术知识和经验,需要仔细考虑各种因素,如性能、可靠性和安全性等。 ### 回答2: 主从复制是一种实现高可用性、数据冗余备份的方案,也是一种扩展MySQL读操作能力的方式之一。在您的应用程序环境中可以将主数据库用于写操作,从数据库用于读操作,以提高数据库的性能和可用性。本文将介绍如何在Linux环境中部署主从数据库。 一、安装MySQL 首先在两台服务器上分别安装MySQL数据库。可使用以下命令安装: $ sudo apt-get update $ sudo apt-get install mysql-server 在安装MySQL时需要指定MySQL的密码和确认该密码。 二、配置主服务器 在主服务器上配置MySQL以使其启用主从复制功能。编辑MySQL配置文件/etc/mysql/mysql.conf.d/mysqld.cnf(或者/etc/mysql/my.cnf): $ vim /etc/mysql/mysql.conf.d/mysqld.cnf 在这里指定MySQL服务器在启动时所监听的IP地址和端口号: bind-address=0.0.0.0 port=3306 同时开启二进制日志功能: log-bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M 重启MySQL服务器让这些设置生效: $ sudo systemctl restart mysql 三、创建从库复制用户 在主服务器上创建用于从服务器复制数据的引擎用户。可使用以下命令: mysql> CREATE USER 'slave'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%'; FLUSH PRIVILEGES; 该用户只需要复制数据,因此只在访问复制的数据时所需的权限即可。 四、备份主服务器上的数据 在主服务器上必须备份MySQL数据库。可使用以下命令: $ mysqldump -u root -p --all-databases --lock-all-tables > db_backup.sql 此命令将所有数据库和所有表都备份到文件db_backup.sql中。 五、配置从服务器 在从服务器上配置MySQL以复制主服务器上的数据。编辑MySQL配置文件/etc/mysql/mysql.conf.d/mysqld.cnf(或者/etc/mysql/my.cnf): $ vim /etc/mysql/mysql.conf.d/mysqld.cnf 本例中,我们使用IP地址127.0.0.1作为MySQL的监听地址。这意味着从服务器将只接受来自本地主机的连接: bind-address=127.0.0.1 port=3306 启用从服务器的日志(用于调试目的): log-bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M 然后重启MySQL使其生效: $ sudo systemctl restart mysql 六、指定主服务器和相关信息 在从服务器上指定主服务器。可使用以下命令: mysql> STOP SLAVE; mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.101', MASTER_USER='slave', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107; mysql> START SLAVE; 此命令的第一行停止从服务器的复制功能。 CHANGE MASTER语句更新复制从服务器的主服务器的详细信息。在MASTER_LOG_FILE和MASTER_LOG_POS参数中指定用于配置复制的位置。这里,我们指定从服务器应该从主服务器的第一个二进制日志文件开始复制(mysql-bin.000001),并从文件中的第107个位置开始(MASTER_LOG_POS)。 最后,通过使用START SLAVE命令启用从服务器的复制功能。 七、检查从服务器状态 在从服务器上使用以下命令检查从服务器的状态: mysql> SHOW SLAVE STATUS\G 如果看到Retry column等于0,则说明复制正在正常地进行中。如果您看到一些错误代码,请查看MySQL官方文档以了解如何排除故障。 以上是在Linux环境中部署主从数据库的详细步骤,当然,具体部署可能有所差异。在实际操作中应结合自己场景进行调整和完善,使得主从复制技术更好的发挥其优势。 ### 回答3: 在Linux系统中,如何部署主从数据库主从数据库部署是为了实现数据库的高可用性和容错性。在主从数据库中,主服务器负责写入操作,从服务器负责读取操作。当主服务器宕机或发生故障时,系统会自动切换到从服务器,确保服务的持续运行。下面我们详细介绍如何在Linux系统中部署主从数据库。 首先,我们需要安装数据库软件。MySQL是一种流行的数据库软件,支持主从数据库部署。我们可以使用以下命令在Linux系统中安装MySQL: ``` sudo apt-get update sudo apt-get install mysql-server ``` 安装完成后,我们需要进行一些配置,使MySQL支持主从数据库部署。首先,在主服务器上修改my.cnf配置文件,添加以下内容: ``` server-id = 1 log_bin = /var/log/mysql/mysql-bin.log binlog_do_db = exampledb ``` 其中,server-id是一个唯一的标识,必须在主从服务器中唯一;log_bin是二进制日志文件的路径,用于记录所有的写入操作;binlog_do_db指定需要进行主从同步的数据库。 接着,在从服务器上修改my.cnf配置文件,添加以下内容: ``` server-id = 2 relay-log = /var/log/mysql/mysql-relay-bin.log log_slave_updates = 1 read_only = 1 ``` 其中,server-id同样是一个唯一的标识;relay-log是从服务器上的中继日志文件,用于记录主服务器发送过来的日志;log_slave_updates表示从服务器是否记录自己的写入操作;read_only表示从服务器是否只读。 然后我们需要在主服务器上创建一个用于同步的用户,并授权其访问主服务器上的数据库。我们可以使用以下命令: ``` CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; FLUSH PRIVILEGES; ``` 其中,repl是用于同步的用户,password是密码。 接着,在从服务器上启动从服务器,执行以下命令: ``` CHANGE MASTER TO MASTER_HOST='master_host_ip', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='bin_log.000001', MASTER_LOG_POS=4; START SLAVE; ``` 其中,master_host_ip是主服务器的IP地址;repl和password分别是之前创建的用户和密码;bin-log.000001是主服务器上的二进制日志文件名,MASTER_LOG_POS是主服务器上的位置信息。 执行完以上命令后,在从服务器上可以通过以下命令查看从服务器状态: ``` SHOW SLAVE STATUS \G ``` 如果状态中的Slave_IO_Running和Slave_SQL_Running都显示Yes,则说明主从同步已经成功进行。 总结一下,在Linux系统中部署主从数据库的步骤如下: 1. 安装MySQL数据库软件。 2. 在主服务器上修改my.cnf配置文件,添加server-id、log_bin和binlog_do_db参数。 3. 在从服务器上修改my.cnf配置文件,添加server-id、relay-log、log_slave_updates和read_only参数。 4. 在主服务器上创建用于同步的用户,并授权访问权限。 5. 在从服务器上启动从服务器,并执行CHANGE MASTER TO和START SLAVE命令。 6. 查看从服务器状态,确认主从同步已经成功进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值