MYSQL简单主从复制

master:172.25.44.1

slave:172.25.44.2

mysql5.7安装

master和slave均操作

准备rpm包:mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar

解压:tar xvf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar

[root@server1 software]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
[root@server1 software]# tar xvf mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar
mysql-community-test-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-client-5.7.17-1.el6.x86_64.rpm
mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm
[root@server1 software]# ls
mysql-5.7.17-1.el6.x86_64.rpm-bundle.tar mysql-community-embedded-devel-5.7.17-1.el6.x86_64.rpm
mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm
mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm
mysql-community-devel-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm
mysql-community-embedded-5.7.17-1.el6.x86_64.rpm mysql-community-test-5.7.17-1.el6.x86_64.rpm

查看之前是否安装过mysql,如果有要卸载掉

[root@server1 software]# rpm -qa |grep mysql
mysql-libs-5.1.71-1.el6.x86_64
[root@server1 software]# rpm -e mysql-libs-5.1.71-1.el6.x86_64
error: Failed dependencies:
libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
mysql-libs is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64
[root@server1 software]# rpm -e mysql-libs-5.1.71-1.el6.x86_64 --nodeps
[root@server1 software]# rpm -qa |grep mysql

安装mysql的rpm包(yum命令自动解决依赖问题,但是如果使用rpm安装的话,需要安装numactl和libaio这两个依赖包)

[root@server1 software]# yum install mysql-community-common-5.7.17-1.el6.x86_64.rpm mysql-community-libs-5.7.17-1.el6.x86_64.rpm mysql-community-devel-5.7.17-1.el6.x86_64.rpm mysql-community-client-5.7.17-1.el6.x86_64.rpm mysql-community-server-5.7.17-1.el6.x86_64.rpm -y

Installed:
mysql-community-client.x86_64 0:5.7.17-1.el6 mysql-community-common.x86_64 0:5.7.17-1.el6
mysql-community-devel.x86_64 0:5.7.17-1.el6 mysql-community-libs.x86_64 0:5.7.17-1.el6
mysql-community-server.x86_64 0:5.7.17-1.el6 Dependency Installed:
libaio.x86_64 0:0.3.107-10.el6 numactl.x86_64 0:2.0.7-8.el6 Complete!

启动mysql

[root@server1 software]# /etc/init.d/mysqld start
Initializing MySQL database: [ OK ]
Installing validate password plugin: [ OK ]
Starting mysqld: [ OK ]
注意:当然其实也是可以不删除那个系统原装的libs库的:
这样的话就需要装mysql-community-libs-compat-5.7.17-1.el6.x86_64.rpm这个兼容库,装这个兼容库的时候会自动删除系统原装的libs库。

/etc/mysql.cnf

master:
log-bin=mysql-bin
server-id=1
binlog-do-db=test #允许主从复制的库
binlog-ignore-db=mysql #禁止主从复制的库
[root@server1 software]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]
slave:
server_id=2
[root@server2 software]# /etc/init.d/mysqld restart
Stopping mysqld: [ OK ]
Starting mysqld: [ OK ]

mysql操作

初次登陆mysql并修改密码

mysql5.7的版在数据初始化的时候就会等成一个初始密码,记录在/var/log/mysqld.log

[root@server1 software]# grep "temporary password" /var/log/mysqld.log
2017-06-13T10:13:50.746938Z 1 [Note] A temporary password is generated for root@localhost: ubP0Wisi>HJg

运行mysql_secure_installation会执行几个设置:

a)为root用户设置密码

b)删除匿名账号

c)取消root用户远程登录

d)删除test库和对test库的访问权限

e)刷新授权表使修改生效

通过这几项的设置能够提高mysql库的安全。建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,详细步骤请参看下面的命令:

[root@server1 software]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root. Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success. All done!
master配置

grant replication slave on . to root@172.25.44.2 identified by 'Lt@18392027447';##通过密码赋予172.25.44.2的root用户权限

flush privileges;##刷新mysql的系统权限相关表

[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant replication slave on *.* to root@172.25.44.2 identified by 'Lt@18392027447';
Query OK, 0 rows affected, 1 warning (0.11 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.40 sec) mysql> quit
Bye
[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1399 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> quit
Bye
slave配置

master_log_file='mysql-bin.000001',master_log_pos=1399;##master端mysql> show master status;可查询此参数

show slave status\GSlave_IO和Slave_SQL必须开启,即Slave_IO_Running和Slave_SQL_Running必须对应yes

[root@server2 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> change master to master_user='root',master_password='Lt@18392027447',master_host='172.25.44.1',master_log_file='mysql-bin.000001',master_log_pos=1399;
Query OK, 0 rows affected, 2 warnings (1.23 sec) mysql> start slave;
Query OK, 0 rows affected (0.14 sec) mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.25.44.1
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1399
Relay_Log_File: server2-relay-bin.000002
Relay_Log_Pos: 320
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: 1399
Relay_Log_Space: 529
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: 1
Master_UUID: ff0d1657-5020-11e7-9d4a-525400a2b9df
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec) mysql> quit
Bye

主从服务器测试:

master端建立数据库并插入一条数据:
[root@server1 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> create database westos;
Query OK, 1 row affected (0.18 sec) mysql> use westos;
Database changed
mysql> create table westos(name char(10),id int(3));
Query OK, 0 rows affected (1.64 sec) mysql> insert into westos values('kiosk',003);
Query OK, 1 row affected (0.13 sec) mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec) mysql> quit
Bye
slave端数据库查询
[root@server2 software]# mysql -uroot -pLt@18392027447
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.17 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| westos |
+--------------------+
5 rows in set (0.00 sec) mysql> use westos;
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
mysql> select * from westos;
+-------+------+
| name | id |
+-------+------+
| kiosk | 3 |
+-------+------+
1 row in set (0.00 sec) mysql> quit
Bye

mysql简单主从复制(一)的更多相关文章

  1. mysql简单主从复制(二)

    mysql的简单主从复制 主从复制打开后,各自开启的线程: master端 mysql> show processlist\G; *************************** 1. r ...

  2. liinux安装 mysql 及主从复制

    mariadb其实就是mysqlmysql已经被oracle收购,它即将闭源,马上要开始收费了因此还想免费试用开源的数据库mysql,就在centos7上,将mysql分支为mariadb 安装mar ...

  3. mysql (主从复制)(proxy , Amoeba)

    原址如下: http://heylinux.com/archives/1004.html Mysql作为目前世界上使用最广泛的免费数据库,相信所有从事系统运维的工程师都一定接触过.但在实际的生产环境中 ...

  4. 使用 Xtrabackup 在线对MySQL做主从复制【转】

    1. 说明 1.1 xtrabackup mysqldump对于导出10G以下的数据库或几个表,还是适用的,而且更快捷.一旦数据量达到100-500G,无论是对原库的压力还是导出的性能,mysqldu ...

  5. mysql的主从复制原理与实现

    关于mysql的主从复制,之前一直在听说这个话题,一直没有实现,昨天学习了下,原来是这么回事: 既然是主从复制,那么肯定有主有从,也就说一个主数据库(一般为写库),一个从数据库(读库).主数据库更新了 ...

  6. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(上) 上周BOSS给分配任务让实现一下Mysql数据库的主从复制以及读写分离,然后花了一盏茶的功夫进行了调研,发现主从复制数据库进行一番 ...

  7. Linux 下的mysql+centos7+主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

  8. mysql实现主从复制/主从同步

    业务场景 小公司业务代码存于一个服务器上,而这个服务器有的时候回宕机,导致业务停顿,造成影响.这个时候 就需要做高可用 两个ngix+两个tomcat+两个mysql实现高可用,避免单点问题.中间使用 ...

  9. redis 和 mysql 的主从复制

    1. mysql主从复制 (1) 为什么要做主从复制? 1.在业务复杂的系统中,有这么一个情景,有一句sql语句需要锁表,导致暂时不能使用读的服务,那么就很影响运行中的业务,使用主从复制,让主库负责写 ...

随机推荐

  1. struts2.0的工作原理?

    struts2并不是一个陌生的web框架,它是以Webwork的设计思想为核心,吸收struts1的优点,可以说 struts2是struts1和Webwork结合的产物. struts2 的工作原理 ...

  2. .Net 内存池

    最近在开发 Pinpoint .Net 客户端,和服务端通信都是通过 TCP 或者 UDP,需要处理大量的 Byte 数据,使用 .Net Framework 只能通过 new Byte[] 的方式申 ...

  3. ARKit 初体验

    ARKIT是苹果公司在今年发布的一个AR开发包,用于现有的IOS设备,是的,就是用在手机或者平板上,类似于pokemon go的效果.看了下演示视屏,嗯,看起来很厉害. 对于一个资深软粉,居然被要求研 ...

  4. 【Mysql】MySQL与Oracle的大小写问题

    转载来源:http://aofengblog.blog.163.com/blog/static/63170212010101065030136/ MySQL与Oracle在大小写处理上的区别: 1MY ...

  5. Mybatis Dynamic Query 框架整合

    项目地址:https://github.com/wz2cool/mybatis-dynamic-query 文档地址:https://wz2cool.gitbooks.io/mybatis-dynam ...

  6. java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-【费元星Q9715234】

    java+反射+多线程+生产者消费者模式+读取xml(SAX)入数据库mysql-[费元星Q9715234] 说明如下,不懂的问题直接我[费元星Q9715234] 1.反射的意义在于不将xml tag ...

  7. 接口自动化(Python)-利用正则表达式从返回的HTML文本中截取自己想要的值

    例如一个功能接口的返回值 是一个HTML文本: 例如我们要取上图中标识的id的值,并且这个值是动态的,这是我们就需要用到正则表达式进行匹配. 如下是正则匹配的方法(代码中都有注释): 我们在真正使用的 ...

  8. ASP.NET Core MVC Tag Helpers 介绍

    简介 Tag Helpers 提供了在视图中更改和增强现有HTML元素的功能.将它们添加到视图中,会经过Razor模板引擎处理并创建一个HTML,之后再返回给浏览器.有一些Tag Helpers,其实 ...

  9. 四、什么是vuex

    一.关于vuex刚开始学习的时候对于里面的很多名词有很陌生.很难接受这个定义,下面这个链接很好很简单通俗的解释了什么是vuex 我喜欢的vuex网址:https://zhuanlan.zhihu.co ...

  10. Spring Boot整合Dubbo使用及开发笔记

    一.概述: Spring Dubbo是我开发的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...