MYSQL之一主多从搭建方案

# 安装前必须删除原来的安装,需要检查 以下文件是否存在 ,如果存在则要删除
# /etc/my.cnf
# /etc/init.d/mysqld
# 添加mysql依赖库
shell> yum search libaio # search for info
shell> yum install libaio # install library
# 创建mysql 与用户组,-s /bin/false 表示该用户不能登录
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
# 创建安装目录(后面会用到,随意命名即可)
shell> mkdir -p package
shell> cd package
shell> pwd
/package
# 解压安装包至当前目录并给定软连接
shell> tar zxvf mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz 
shell> ln -s mysql-5.5.60-linux-glibc2.12-x86_64 mysql 
shell> cd mysql
# 为 mysql 用户添加权限
shell> chown -R mysql ./
shell> chgrp -R mysql ./ 
# 创建data目录并添加权限
shell> mkdir -p /data/mysql
shell> chown -R mysql:mysql /data/mysql
# 拷贝配置文件
shell> cp support-files/my-medium.cnf /etc/my.cnf
shell> cp support-files/mysql.server /etc/init.d/mysqll

# 修改配置
shell> vi /etc/my.cnf
# 添加或修改以下内容即可
[mysqld]
basedir=/package/mysql #mysql解压目录
datadir=/data/mysql  #mysql数据文件目录
character-set-server=utf8  #字符集
# 初始化mysql数据库
shell> ./scripts/mysql_install_db --user=mysql --basedir=/package/mysql --datadir=/data/mysql
# 添加环境变量(文件最后一行,新增一行添加即可)
shell> vi /etc/profile
PATH=/home/cbt/svr/mysql/bin:$PATH
export PATH 
# 环境变量立即生效
shell> source /etc/profile
# 启动数据库
shell> service mysql start
# 开机启动
shell> chkconfig mysqld on
# 初始化mysql的一些设置
shell> mysql_secure_installation  
#回车
Enter current password for root (enter for none):
#y,设置mysql的root密码
Set root password?[Y/n] y
#以下都yes
Remove anonymous users?[Y/n] y
Disallow root login remotely?[Y/n] y
Remove test database and access to it?[Y/n] y
Reload privilege tables now?[Y/n] y
ThanksforusingMySQL!
# 允许远程登陆
mysql>use mysql;
mysql>select host,user,password from user;
mysql>update user set password=password('123456') where user='root'; 
mysql>update user set host='%' where user='root' and host='localhost'; 
mysql>flush privileges; 
# 安装时的一些错误
-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: 没有那个文件或目录
解决: yum -y install perl perl-devel
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决:yum -y install libaio-devel



#################### 一主多从配置 ####################
master my.cnf 配置----- 

shell> vi /etc/my.cnf
# binlog 格式
binlog-format=ROW 
log-bin=mysql-master-bin 
# slave更新时是否记录到日志中;
log-slave-updates=true
# 开启半同步 
rpl_semi_sync_master_enabled=ON
# 需要同步的二进制数据库名;
binlog-do-db=health  #需要同步的数据库
# 不同步的二进制数据库名,如果不设置可以将其注释掉;
binlog-ignore-db=information_schema  #不需要同步的数据库
binlog-ignore-db=mysql
binlog-ignore-db=personalsite
binlog-ignore-db=test
# 保存并重启mysql服务
shell> service mysql restart
# 登陆数据库
shell> mysql -uroot -p123456
# 创建用户用于主从同步的数据库
mysql>grant replication slave,super,reload on *.* to slave1@192.168.153.12 identified by '123456';
# 查看主节点状态
mysql>show master status 
# 在主库上查看已连接的slave主机
mysql>show slave hosts;
# 查看所有binlog日志 
mysql>show binary logs;
# 查看所有binlog 事件
mysql>show binlog events in 'mysql-bin.000003';

# slave my.cnf 配置-----

server-id = 2
log-bin=mysql-slave-bin
binlog-do-db=health #需要同步的数据库
binlog-ignore-db=information_schema #不需要需要同步的数据库
binlog-ignore-db=mysql
binlog-ignore-db=personalsite
binlog-ignore-db=test
# 保存并重启slave节点mysql服务
shell> service mysql restart
# 登陆slave服务器mysql数据库
shell> mysql -uroot -p123456
# slave节点修改master配置
mysql>change master to master_host='192.168.153.11', master_user='slave1', master_password='123456';
# Slave 相关操作
# 启动暂停slave
mysql>start slave;
mysql>stop slave;
# 查看 slave 状态
mysql>show slave status \G;

这里写图片描述

posted @ 2018-07-15 19:51  Nihility丶  阅读(48)  评论(0编辑  收藏  举报