Centos6.8 搭建 Mysql 主从复制

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 实例环境:  MySQL-Master:Centos-6.8:192.168.153.130  MySQL-Slave:Centos-6.8:192.168.153.131 1、两台服务器安装mysqlyum -y install mysql mysql-server2、打开my...

 

实例环境:

  MySQL-Master:Centos-6.8:192.168.153.130

  MySQL-Slave:Centos-6.8:192.168.153.131

 

1、两台服务器安装mysql

yum -y install mysql mysql-server

2、打开mysqld服务并设置密码

[root@localhost /]# service mysqld start
初始化 MySQL 数据库: Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

                                                           [确定]
正在启动 mysqld:                                          [确定]

[root@localhost /]# mysql_secure_installation    #此命令来设置mysql root密码以及其他




NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!


In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, 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 MySQL
root user without the proper authorisation.

Set root password? [Y/n]          #单击回车,或者输入Y点击回车
New password:       #输入需要设置的密码
Re-enter new password:      #再次输入需要设置的密码
Password updated successfully!     #下面一路y就可以,也可以根据自己的需求来选择
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] 
 ... 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] 
 ... Success!

By default, MySQL 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] 
 - 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] 
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

3、修改mysqld配置文件,修改mysql-主配置文件

[root@localhost /]# vim /etc/my.cnf 
添加以下配置
server-id=1
log-bin=mysql-bin #这个一定得设置,否则没有日志的话,从数据库上会报错
[root@localhost /]# service mysqld stop
停止 mysqld:                                          [确定]
[root@localhost /]# service mysqld start
正在启动 mysqld:                                          [确定]
[root@localhost /]# service mysqld status
mysqld (pid  3682) 正在运行...

4、创建主从复制账号

mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO backup@'192.168.153.131' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

#192.168.153.131:从服务器地址 
#backup是授权给从服务器链接的数据库账户
#123456是密码

5、解锁表

mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

6、正确状态应该如下

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      275 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

7、修改数据库配置,修改从库设置

[root@localhost /]# vim /etc/my.cnf 
[mysqld]
添加语句
log-bin = mysql-bin
server_id = 2
master-host = 192.168.137.33
master-user = backup
master-pass = 123456
master-port = 3306
master-connect-retry = 60

8、进入数据库开始配置

mysql -u root -p
Enter password:
#开始配置
mysql> CHANGE MASTER TO
     MASTER_HOST='192.168.153.130', #主服务器地址
     MASTER_USER='backup',    #主服务器授权的账户
     MASTER_PASSWORD='123456',  #主服务器授权的账户密码
     MASTER_PORT=3306,   #数据库端口
     MASTER_LOG_FILE='mysql-bin.000001',  #主服务器log-file
     MASTER_LOG_POS=251,    #主服务器Position
     MASTER_CONNECT_RETRY=10;  #重新连接时间
Query OK, 0 rows affected (0.02 sec)

  如果上一步出现失败,查看是否slave服务已经打开,如果发现打开,先关闭slave服务才能继续进行配置

9、测试主从是否配置好

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.153.130
                  Master_User: backup
                  Master_Port: 3306
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 275
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes  #这两个一定要是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: 275
              Relay_Log_Space: 407
              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: 
1 row in set (0.00 sec)

10、数据库测试:

在主服务器上创建test数据库

mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> 

在从服务器上查看下是否有test数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

mysql> 

至此,就完成了主从复制的服务。

主从复制完成,但是还不能满足我们的需求。这种配置方式只是将主库的数据同步到从库。
我们设想下:有一天我们往从库插入或者删除数据,那么主库的数据不会得到改变。就会造成数据不一致。
 
如果想配置主从双向同步,点击跳转: Mysql主从双向同步
如果想配置读写分离,点击跳转:Mysql读写分离

 

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
21天前
|
安全 关系型数据库 MySQL
Linux(CentOS6)安装MySQL5.6
Linux(CentOS 6)系统上安装MySQL 5.6版本的详细步骤,包括准备数据存放目录、创建用户、下载安装包、初始化数据库、配置服务脚本、设置环境变量等操作。
55 1
|
24天前
|
关系型数据库 MySQL Java
centos7安装mysql教程及Navicat平替软件
【8月更文挑战第17天】本教程详述CentOS 7上安装MySQL的过程。首先确保移除任何预装的MySQL组件,然后通过wget获取并安装MySQL的YUM源。可以选择安装特定版本如5.7或8.0。安装MySQL服务器后,启动服务并查找初始密码。登录MySQL后应立即更改密码,并可根据需要设置远程访问权限。此外,还推荐使用免费开源的DBeaver作为数据库管理工具,提供了安装步骤以方便管理和操作MySQL数据库。
|
12天前
|
弹性计算 关系型数据库 MySQL
centos7 mysql安装及配置
本文详细介绍了在阿里云服务器ECS上通过yum源安装MySQL 8.0.12的过程,包括更新yum源、下载并安装MySQL源、解决安装过程中可能遇到的问题等步骤。此外,还介绍了如何启动MySQL服务、设置开机自启、配置登录密码、添加远程登录用户以及处理远程连接异常等问题。适合初学者参考,帮助快速搭建MySQL环境。
86 8
centos7 mysql安装及配置
|
18天前
|
SQL 关系型数据库 MySQL
说一下MySQL主从复制的原理?
【8月更文挑战第24天】说一下MySQL主从复制的原理?
45 0
|
19天前
|
存储 关系型数据库 MySQL
实战!MySQL主从复制一键搭建脚本分享
实战!MySQL主从复制一键搭建脚本分享
26 2
|
20天前
|
SQL 关系型数据库 MySQL
在Linux中,mysql 如何减少主从复制延迟?
在Linux中,mysql 如何减少主从复制延迟?
|
28天前
|
关系型数据库 MySQL Linux
在CentOS上使用yum安装与使用MySQL
在CentOS操作系统上使用yum包管理器安装和配置MySQL数据库的详细步骤,包括下载MySQL的yum源配置、安装MySQL服务、启动和停止服务、设置开机自启动、获取临时密码、修改root用户密码、授权用户以及处理相关问题。同时,文章还包含了一些操作的截图,帮助用户更直观地理解安装和配置过程。
195 0
在CentOS上使用yum安装与使用MySQL
|
28天前
|
关系型数据库 MySQL Linux
在CentOs7虚拟机Linux离线安装mysql5.6(亲测可用)
该博客文章详细记录了在CentOS 7虚拟机上离线安装MySQL 5.6版本的完整过程,包括下载安装包、导入虚拟机、卸载MariaDB、配置文件设置、服务启动和权限配置等步骤。
在CentOs7虚拟机Linux离线安装mysql5.6(亲测可用)
|
1月前
|
关系型数据库 MySQL Linux
【一键解锁神秘力量!】CentOS 7 通过编译源码方式安装 MySQL 数据库 —— 从零到英雄的数据库安装实战秘籍!
【8月更文挑战第9天】随着业务增长,对数据库的需求日益提高。在 CentOS 7 中,通过编译源码安装 MySQL 可提供更高定制性和灵活性。本文详细介绍从准备环境、下载源码、配置编译参数到安装 MySQL 的全过程,并对比 RPM 包安装方法,帮助读者根据需求选择合适方案。实践时需注意备份数据、选择合适版本、确保安全性和调优性能等要点。
102 1
|
1月前
|
SQL 关系型数据库 MySQL
配置MySQL主从复制(一主一从)
配置MySQL主从复制(一主一从)