参考:

https://www.cnblogs.com/yinzhengjie/p/10371899.html

https://www.sysit.cn/blog/post/sysit/CDH6.2.0%E7%B3%BB%E7%BB%9F%E9%83%A8%E7%BD%B2%E6%89%8B%E5%86%8C

搭建主库: CM节点

mysql> status;

Server version:5.7.27 MySQL Community Server (GPL)

#配置源

cat > /etc/yum.repos.d/mysql-community-el7.repo<<'EOF'

[mysql57-community]

name=MySQL 5.7 Community Server

baseurl=http://mirror.tuna.tsinghua.edu.cn/mysql/yum/mysql57-community-el7/

enabled=1

gpgcheck=0

EOF

# 安装

yum install mysql-community-server -y

echo "character-set-server=utf8" >>/etc/my.cnf

systemctl restart mysqld

# mysqld.log中查询密码

grep password /var/log/mysqld.log

2019-04-09T07:28:44.292450Z 1 [Note] A temporary password is generated for root@localhost: wg:V45ci4ljpz!r

#注意,安装CDH最好用官方推进的my.cnf文件,见下附录.

# 测试环境关闭强策略密码,生产环境建议开启。不关闭只支持mysql -uslave -P 3306 -h10.52.2.128 -p的登陆方式

echo "validate-password=off" >>/etc/my.cnf

echo "server-id=101"  >> /etc/my.cnf

echo "log-bin=mysql-bin" >>  /etc/my.cnf                 #开启日志做主从,不然show master status时会是空的.

# 重启mysql

systemctl restart mysqld

# 重置安全配置

mysql_secure_installation

# 登录mysql

mysql -uroot -p

#此时需要用到上面的密码

#登陆后修改成自己的密码:

mysql> alter user user() identified by 'test123456';

#检查

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

symbolic-links=0

log-error=/var/log/mysqld.log
#pid-file=/var/run/mysqld/mysqld.pid
character-set-server=utf8
validate-password=off
server-id=101

#查看当前服务器的server_id

mysql> show variables like '%server_id%';

#查bin_log是否开启

mysql> show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name | Value |
+---------------------------------+-------+
| log_bin | ON
---------------------

#查看授权情况

select user,host from mysql.user;

附录:cloudera官方推荐

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
transaction-isolation = READ-COMMITTED
# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
symbolic-links = 0

key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1

max_connections = 550
#expire_logs_days = 10
#max_binlog_size = 100M

#log_bin should be on a disk with enough free space.
#Replace '/var/lib/mysql/mysql_binary_log' with an appropriate path for your
#system and chown the specified folder to the mysql user.
log_bin=/var/lib/mysql/mysql_binary_log                 #开启bin_log

#In later versions of MySQL, if you enable the binary log and do not set
#a server_id, MySQL will not start. The server_id must be unique within
#the replicating group.
server_id=1

binlog_format = mixed

read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M

# InnoDB settings
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

sql_mode=STRICT_ALL_TABLES

在另一个节点上搭建从库:

操作同上, server-id=102要不同

mysql> show variables like '%server_id%';

配置主从同步:

#在主库上授权copy帐号用于复制,密码是fengfeng99
[root@node106.fengfeng.org.cn ~]# mysql -uroot -p

mysql> CREATE USER 'copy'@'195.189.142.83%' IDENTIFIED BY 'fengfeng99';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'copy'@'195.189.142.83%';
Query OK, 0 rows affected (0.00 sec)

mysql> quit

检查:
#在从库节点上用copy用户远程:
mysql -u copy -pfengfeng99 -P 3306 -h195.189.142.89
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.00 sec)

#获取主库的日志信息并生成主库数据镜像,主库上:

mysql> FLUSH TABLES WITH READ LOCK;                  #对主库上所有表加锁,停止修改,即在从库复制的过程中主库不能执行UPDATA,DELETE,INSERT语句!
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW MASTER STATUS;                    #获取主库的日志信息,file表示当前日志文件名称,position表示当前日志的位置
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> quit

[root@zhep-opay-temp-1 ~]# mysqldump --all-databases --master-data -u root -pfengfeng99 -P 3306 > fengfeng-master.db  #另开一个终端生成镜像,在生成完成之前不要释放锁。
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@zhep-opay-temp-1 ~]# ls
anaconda-ks.cfg fengfeng-master.db original-ks.cfg test.log

mysql> UNLOCK TABLES;      #主库数据生成镜像完毕后,我们需要把主库的锁释放掉,需要注意的是,在上锁这一段期间,我们无法对数据库进行写操作,比如UPDATA,DELETE,INSERT。
Query OK, 0 rows affected (0.00 sec)

将主库的镜像拷贝到从库服务器,让从库应用主库镜像
[root@zhep-opay-temp-1 ~]# scp fengfeng-master.db 195.189.142.83:~
fengfeng-master.db

登陆从库:

mysql> source fengfeng-master.db;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

#在从库上建立复制关系,即从库指定主库的日志信息和链接信息
mysql>
CHANGE MASTER TO
MASTER_HOST='195.189.142.89',
MASTER_PORT=3306,
MASTER_USER='copy',
MASTER_PASSWORD='fengfeng99',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;

#MASTER_LOG_FILE是上面SHOW MASTER STATUS里的file, POS里面的position

mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: node106.fengfeng.org.cn
Master_User: copy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: fengfeng-mysql-bin.000002
Read_Master_Log_Pos: 4095
Relay_Log_File: node107-relay-bin.000002
Relay_Log_Pos: 332
Relay_Master_Log_File: fengfeng-mysql-bin.000002
Slave_IO_Running: Yes     #观察IO进程是否为yes,如果为yes说明正常,如果长时间处于"Connecting"状态就得检查你的从库指定的主库的链接信息是否正确
Slave_SQL_Running: Yes    #观察SQL进程是否为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: 4095
Relay_Log_Space: 541
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   #该参数表示从库和主库有多少秒的延迟,咱们可以理解为再过多少秒数据和主库保持一致,如果为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: 106
Master_UUID: 74227047-8b60-11e9-8cba-000c29985293
Master_Info_File: /fengfeng/softwares/mysql/data/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)

#测试主从是否同步:
主库上建一个test库
mysql> CREATE DATABASE test DEFAULT CHARACTER SET = utf8;
Query OK, 1 row affected (1.82 sec)

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

检查从库是否同步:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)

#也可在主库上建一张表,看是否同步:

create table student(
id int(4) not null AUTO_INCREMENT,
name char(20) not null,
age tinyint(2) NOT NULL default '0',
dept varchar(16) default NULL,
primary key(id),
KEY index_name (name)
);

#插入数据

insert into student(id,name) values(1,'oldboy');

#注:

锁库创建从库:即时在主库上拿一个完整备份,并恢复到新建的从库,优点:可以支持非事务引擎,
缺点:较慢,影响主库使用。
不锁库创建从库:拿主库最近一次可用的历史完整备份,先导数据,再追日志,优点:较为快速,不影响主库,缺点:不支持非事务引擎。

排错:

如果主从不同步.其它都不靠谱,靠谱的是在从库上stop slave; 按上面的方法重做一次.

可以先看错误日志,在: tail -1000 /var/log/mysqld.log

binglog的位置在my.cnf中的:log_bin=/var/lib/mysq

mysql> SHOW BINARY LOGS;
+-------------------------+-----------+
| Log_name | File_size |
+-------------------------+-----------+
| mysql_binary_log.000001 | 181956511 |
| mysql_binary_log.000002 | 526878457 |
| mysql_binary_log.000003 | 2055720 |
+-------------------------+-----------+
3 rows in set (0.00 sec)

#把从库设置成readonly

mysql> show global variables like "%read_only%";
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | OFF |
| super_read_only | OFF |
| transaction_read_only | OFF |
| tx_read_only | OFF |
+-----------------------+-------+
对于需要保证master-slave主从同步的salve库,如果要设置为只读状态,需要执行的命令为:
mysql> set global read_only=1;

将salve库从只读状态变为读写状态,需要执行的命令是:
mysql> set global read_only=0;

grant all on *.* to webdev@'localhost' identified by 'Test12345@';
grant select on *.* to webdev2@'localhost' identified by 'Test12345@';
grant select on *.* to webdev2@'%' identified by 'Test12345@';

CDH的mysql主从准备的更多相关文章

  1. 在 CentOS7 上部署 MySQL 主从

    在 CentOS7 上部署 MySQL 主从 通过 SecureCRT 连接至 MySQL 主服务器: 找到 my.cnf 文件所在的目录: mysql --help | grep my.cnf 一般 ...

  2. 高性能Mysql主从架构的复制原理及配置详解

    温习<高性能MySQL>的复制篇. 1 复制概述 Mysql内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台 ...

  3. MySQL主从同步

    脚本 [root@test scripts]# cat ss.sh #!/bin/bash . /etc/init.d/functions MYUSER=root MYPASS=c565f972 SO ...

  4. mysql主从配置

    引言: 双11,阿里云服务器打折,于是我忍不住又买了一台服务器,于是咱也是有两台服务器的爷们了,既然有了两台服务器,那么肯定要好好利用一下吧,那么就来玩玩mysql的主从配置吧. 准备 两台数据库服务 ...

  5. 恢复MySQL主从数据一致性的总结

    今日上午,同事告知,MySQL主从数据库的数据不一致,猜测备库在同步过程中出现了问题,于是,登上备库,使用 mysql> show slave status\G查看,果然,备库在insert语句 ...

  6. LVS+Keepalived+Squid+Nginx+MySQL主从高性能集群架构部署方案

    方案一,在tomcat的workers.properties里面配置相关条件 worker.tomcat.lbfactor= worker.tomcat.cachesize= worker.tomca ...

  7. Mysql主从配置,实现读写分离

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  8. MySQL 主从配置

    mysql主从复制指两个服务器之间数据库的同步,当主服务器的数据进行了变更,从服务器也会自动更新,其过程是通过bin-log日志实现的,本质是binlog日志的传输. mysql主从分两个角色 1.主 ...

  9. MySQL主从同步配置

    如果主从配置之前安装了云平台,请停止云平台后在进行mysql主从配置. 1. 登录master数据库,检查数据库端口防火墙设置,允许远程客户连接,如果没有,执行以下操作. 执行命令:iptables  ...

随机推荐

  1. Python身份运算符

    运算符 描述 实例 is is 是判断两个标识符是不是引用自一个对象 x is y, 类似 id(x) == id(y) , 如果引用的是同一个对象则返回 True,否则返回 False is not ...

  2. hdu 5738 Eureka 极角排序+组合数学

    Eureka Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. Codevs 4019 想越狱的小明

    4019 想越狱的小明 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题目描述 Description 这次小明来到了经典美剧<越狱>的场景里-- 它 ...

  4. java中报错:problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError

    今天和往常一样打开项目,竟然报错problem with class file or dependent class; nested exception is java.lang.NoClassDef ...

  5. 在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib 及一些问题

    一.准备工作: 1.下载OpenCV安装包:https://github.com/opencv/opencv 安装过程实际上就是解压过程,安装完成后得到(这里修改了文件名): 2.下载opencv_c ...

  6. struct streaming中的监听器StreamingQueryListener

    在struct streaming提供了一个类,用来监听流的启动.停止.状态更新 StreamingQueryListener 实例化:StreamingQueryListener 后需要实现3个函数 ...

  7. Android_(自动化)自动获取手机电池的剩余电量

    自动获取手机电池的剩余电量 通过使用BroadcastReceiver的特性来获取手机电池的电量,注册BroadcastReceiver时设置的IntentFilter来获取系统发出的Intent.A ...

  8. MySQL datetime 和 timestamp 的区别

    [转载]:MySQL中有关TIMESTAMP和DATETIME的总结 1. datetime 和 timestamp 的相同点 两者都可以用来表示YYYY-MM-DD HH:MM:SS[.fracti ...

  9. UFLDL(Unsupervised Feature Learning and Deep Learning)

    UFLDL(Unsupervised Feature Learning and Deep Learning)Tutorial 是由 Stanford 大学的 Andrew Ng 教授及其团队编写的一套 ...

  10. Laravel5.2中Eloquent与DB类的区别是什么?

    要了解这些先看看关于数据库组件的那些事儿(就是 Eloquent ORM) 数据库组件大概分了三层: 数据库连接层 查询构造层 应用层 来看一下每一层有哪些东西,分别对应文档的哪一部分: 数据库连接层 ...