centos主从数据库

需求:两台centos虚拟机,网络是通的。
配置yum源则需要:CentOS-7-x86_64-DVD-1511.iso镜像,使用网络yum源则不需要

修改主机名(两个节点)

//修改主机名,也可以永久修改命令:hostnamectl  set-hostname name

[root@localhost ~]# hostname mysql1
[root@localhost ~]# bash

[root@localhost ~]# hostname mysql2
[root@localhost ~]# bash

关闭防火墙及selinux服务(两个节点)

[root@mysql1 ~]# systemctl stop firewalld
[root@mysql1 ~]# setenforce 0

配置hosts文件映射(两个节点)

[root@mysql2 ~]# vi /etc/hosts
//添加以下两行
192.168.174.10 mysql1
192.168.174.20 mysql2
~

配置yum源并安装数据库(两个节点)可以ftp

注:使用网络yum源,则不需要配置,只需跳到下面 下载数据库服务的步骤

[root@mysql1 ~]# mkdir /opt/centos
[root@mysql1 ~]# mount CentOS-7-x86_64-DVD-1511.iso /opt/centos/
mount: /dev/loop0 is write-protected, mounting read-only
[root@mysql1 ~]# cd /etc/yum.repos.d/
[root@mysql1 yum.repos.d]# ls 
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@mysql1 yum.repos.d]# mv * /opt/
[root@mysql1 yum.repos.d]# vi local.repo
[centos]
name=centos
baseurl=file:///opt/centos
enabled=1
gpgcheck=0

清空缓存,查看yum源是否可用(两个节点)

[root@mysql1 yum.repos.d]# yum clean all
Loaded plugins: fastestmirror
Cleaning repos: centos
Cleaning up everything
[root@mysql1 yum.repos.d]# yum repolist
Loaded plugins: fastestmirror
centos                                                   | 3.6 kB     00:00     
(1/2): centos/group_gz                                     | 155 kB   00:00     
(2/2): centos/primary_db                                   | 2.8 MB   00:00     
Determining fastest mirrors
repo id                              repo name                            status
centos                               centos                               3,723
repolist: 3,723

下载数据库并初始化数据库并配置主从数据库(两个节点)

注:主节点先配置

[root@mysql1 ~]# yum install -y mariadb mariadb-server
[root@mysql1 ~]# systemctl start mariadb //启动数据库,不启动是没办法初始化的
[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: 
Re-enter new password: 
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!
[root@mysql1 ~]# vi /etc/my.cnf
//在mysqld下添加以下两行
[mysqld]
log_bin=mysql-bin   //记录操作日志
server_id=10		//数据库集群中的每个节点id不同,普通写成id的最后一段

重启数据库,并进入数据库

[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.65-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.


//在主节点上创建一个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)]> 

配置从节点


[root@mysql2 ~]# systemctl start mariadb    //启动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 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: 
Re-enter new password: 
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!
[root@mysql2 ~]# vi /etc/my.cnf
[mysqld]
log_bin=mysql-bin
server_id=20

[root@mysql2 ~]# systemctl restart mariadb  //配置好文件记得重启数据库
//进入数据库
[root@mysql2 ~]# mysql -u root -p000000
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.65-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)]> change master to master_host='mysql1',master_user='user',master_password='000000';
Query OK, 0 rows affected (0.00 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: mysql1
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 527
               Relay_Log_File: mariadb-relay-bin.000002
                Relay_Log_Pos: 811
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes  //这两处都是yes
            Slave_SQL_Running: Yes  //这两处都是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: 527
              Relay_Log_Space: 1107
              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)

主节点创建数据库,数据表,插入数据


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

MariaDB [(none)]> use mytest;
Database changed
MariaDB [mytest]> create table exam(
    -> id int primary key AUTO_INCREMENT,
    -> name varchar(10) not null,
    -> math double,
    -> chinese double,
    -> english double);
Query OK, 0 rows affected (0.00 sec)

MariaDB [mytest]> insert into exam values
    -> (null,'polo',56,80,75),(null,'rose',82,65,90),(null,'emilly',69,78,50),(null,'elle',78,70,84),(null,'jack',90,85,92);
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

MariaDB [mytest]> select * from exam;
    -> ;
ERROR 1146 (42S02): Table 'mytest.exam;' doesn't exist
MariaDB [mytest]> select * from exam;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  1 | polo   |   56 |      80 |      75 |
|  2 | rose   |   82 |      65 |      90 |
|  3 | emilly |   69 |      78 |      50 |
|  4 | elle   |   78 |      70 |      84 |
|  5 | jack   |   90 |      85 |      92 |
+----+--------+------+---------+---------+
5 rows in set (0.00 sec)

从节点验证查看

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| mytest             |
| performance_schema |
| test               |
| test1              |
+--------------------+
6 rows in set (0.00 sec)

MariaDB [(none)]> use mytest;
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 [mytest]> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| exam             |
+------------------+
1 row in set (0.00 sec)

MariaDB [mytest]> select * from exam;
+----+--------+------+---------+---------+
| id | name   | math | chinese | english |
+----+--------+------+---------+---------+
|  1 | polo   |   56 |      80 |      75 |
|  2 | rose   |   82 |      65 |      90 |
|  3 | emilly |   69 |      78 |      50 |
|  4 | elle   |   78 |      70 |      84 |
|  5 | jack   |   90 |      85 |      92 |
+----+--------+------+---------+---------+
5 rows in set (0.00 sec)

MariaDB [mytest]> 

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

到此主从数据库完成。

有可能遇到的问题:
Slave_SQL_Running: No
1.程序可能在slave上进行了写操作
2.也可能是slave机器重起后,事务回滚造成的
一般是事务回滚造成的:
解决办法:
mysql> slave stop;
mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
mysql> slave start;
第一次操作完毕之后,输入命令showslave status\G ,发现还是没变化,再次操作一次,应该就好。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
步骤如下: 1. 安装MySQL数据库CentOS上安装MySQL,可以使用以下命令: ``` sudo yum install mysql-server ``` 2. 设置MySQL主从复制环境 在MySQL数据库中设置主从复制环境,需要进行以下步骤: - 配置主机MySQL数据库 在主机上的MySQL服务器中,需要设置主机名字和binlog格式。编辑MySQL配置文件`/etc/my.cnf`,并添加以下内容: ``` [mysqld] log-bin=mysql-bin server-id=1 ``` 保存并退出该文件,然后重启MySQL服务器: ``` sudo service mysqld restart ``` - 配置从机MySQL数据库 在从机上的MySQL服务器中,需要设置从机名字和主机信息。编辑MySQL配置文件`/etc/my.cnf`,并添加以下内容: ``` [mysqld] server-id=2 relay-log=mysql-relay-bin log-slave-updates=1 read-only=1 ``` 其中,server-id为从机的编号,需要和主机的编号不同;relay-log和log-slave-updates用于将从机上的binlog更新到从机的relay log中。 保存并退出该文件,然后重启MySQL服务器: ``` sudo service mysqld restart ``` 3. 配置主从复制连接信息 在主机上的MySQL服务器中,需要创建用于从机连接的用户,并授予从机连接的权限。可以使用以下命令: ``` mysql -u root -p ``` 然后,进入MySQL数据库后,执行以下命令: ``` CREATE USER 'slave_user'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'%'; FLUSH PRIVILEGES; ``` 其中,`slave_user`为从机连接用户名,`password`为从机连接密码。 接下来,需要获取主机上的二进制日志文件名和偏移量,即主机的`File`和`Position`值。可以使用以下命令: ``` show master status; ``` 会输出如下信息: ``` +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 120 | | | +------------------+----------+--------------+------------------+ ``` 记录下`File`和`Position`的值,以备后用。 在从机上的MySQL服务器中,需要配置主机的连接信息。可以使用以下命令: ``` mysql -u root -p ``` 然后,进入MySQL数据库后,执行以下命令: ``` CHANGE MASTER TO MASTER_HOST='master_ip_address', MASTER_USER='slave_user', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=120; ``` 其中,`master_ip_address`为主机的IP地址,`slave_user`和`password`为从机连接用户和密码,`mysql-bin.000001`和`120`为主机上的二进制日志文件名和偏移量。 最后,启动从机复制进程: ``` START SLAVE; ``` 至此,主从复制环境已经搭建完成。可以进行测试,验证主从复制是否正常工作。例如,在主机上创建一个数据库和一个表,并插入数据,从机上检查是否同步了这些数据。如果同步了,说明主从复制工作正常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值