实例环境:

  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 '';
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='', #主服务器授权的账户密码
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读写分离

Centos6.8 搭建 Mysql 主从复制的更多相关文章

  1. 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……

    两台主机A.B搭建mysql主从复制关系(A为master,B为slave)后,在slave上执行show slave status,结果中显示Last_IO_Error: error connect ...

  2. [置顶] 两主机搭建MySQL主从复制后,show slave status显示:Last_IO_Error: error connecting to master ……

    两台主机A.B搭建mysql主从复制关系(A为master,B为slave)后,在slave上执行show slave status,结果中显示Last_IO_Error: error connect ...

  3. Docker搭建MySQL主从复制

    Docker搭建MySQL主从复制 主从服务器上分别安装Docker 1.1 Docker 要求 CentOS 系统的内核版本高于 3.10 [root@localhost ~]# uname -r ...

  4. MySQL(14)---Docker搭建MySQL主从复制(一主一从)

    Docker搭建MySQL主从复制(一主一从) 上一篇博客写了MYSQL主从复制原理 : MySQL(13)---MYSQL主从复制原理 这篇我们来写 Docker搭建MYSQL主从复制(一主一从) ...

  5. Linux CentOS 安装MySql以及搭建MySql主从复制

    前言 在之前的博客中,有过几篇都写了关于mysql在linux下的搭建教程,可能以后还会再写,但是又不想重复在写, 于是便想单独将此抽出来,单独写成一篇博客,并详细记录一些安装过程以及遇到的问题解决办 ...

  6. 【大型网站技术实践】初级篇:搭建MySQL主从复制经典架构

    一.业务发展驱动数据发展 随着网站业务的不断发展,用户量的不断增加,数据量成倍地增长,数据库的访问量也呈线性地增长.特别是在用户访问高峰期间,并发访问量突然增大,数据库的负载压力也会增大,如果架构方案 ...

  7. my03_使用空数据库搭建Mysql主从复制

    无数据的主从复制,就搭建一套主从结构的空库,这个是最简单的,先说说这种主从的搭建思路,有利于理解Mysql主从复制1. 安装两套mysql单实例数据库,一个作为主库.一个作为从库:注意要设置两个数据库 ...

  8. 搭建Mysql主从复制

    mysql 主从复制流程图 Server version: 10.0.24-MariaDB-7 Ubuntu 16.04 Master 记录二进制文件 导出数据并记录二进制位置 导入数据,设置二进制位 ...

  9. 基于Docker Compose搭建mysql主从复制(1主2从)

    系统环境 * 3 Ubuntu 16.04 mysql 8.0.12 docker 18.06.1-ce docker-compose 1.23.0-rc3 *3 ==> PS  ###我用的是 ...

随机推荐

  1. 【php】读取"文件列表"按时间倒序显示,并递归显示各层文件夹、!

    思路: 1.读取该php所在文件夹的文件列表,用"改动时间.文件名称"做键值对,塞入数组.对"改动时间"倒序.(貌似不能直接按时间倒序读取文件列表,此处为间接方 ...

  2. Linux fork函数具体图解-同一时候分析一道腾讯笔试题

    原创blog.转载请注明出处 头文件: #include<unistd.h> #include<sys/types.h> 函数原型: pid_t fork( void); (p ...

  3. sanic官方文档解析之logging和request Data

    1,sanic的logging: Sanic允许有做不同类型的日志(通过的日志,错误的日志),在基于Python3的日志API接口请求,你必须具备基本的Python3的日志知识,在你如果想创建一个新的 ...

  4. iOS开发——高级篇——多线程的安全隐患

    资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题   一.解 ...

  5. UVA - 11019 Matrix Matcher hash+KMP

    题目链接:传送门 题解: 枚举每一行,每一行当中连续的y个我们hash 出来 那么一行就是 m - y + 1个hash值,形成的一个新 矩阵 大小是 n*(m - y + 1), 我们要找到x*y这 ...

  6. kill 挂起 Apache Web Server

    [root@hadoop1 ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8 ...

  7. luogu3373 【模板】线段树2

    题目大意: 已知一个数列,你需要进行下面三种操作:1.将某区间每一个数乘上x2.将某区间每一个数加上x3.求出某区间每一个数的和 本线段树的标记是个二元组:add和mul,其代表将一个线段中的每一个点 ...

  8. IE6span 高度问题

    IE6盒子的最小高度为20px,所以设置低于20px大小时,加上_overflow:hidden;

  9. UIPageControll 的属性和用法

    UIPageControll 是继承于UIControl的一个IOS系统UI控件,可以提供给开发者设计分页效果的功能. 初始化方法 UIPageControl * page = [[UIPageCon ...

  10. redis11----监控工具 sentinel

    当master宕机了要把slave1变为master,并把slave2指向slave1. 可以运行时更改master-slave,config set/get,修改一台slave(设为A)为new m ...