mysql5.6主从库安装与配置

mysql5.6主从库安装与配置

关闭防火墙

//临时关闭
systemctl stop firewalld
//禁止开机启动
systemctl disable firewalld

mysql5.6安装

  1. 保证可以联通外网。
  2. 安装wget
yum install wget
  1. 检查系统是否安装其他版本的mysql数据
yum list installed | grep mysql
yum -y remove mysql-libs.x86_64
  1. 添加资源库
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum repolist all | grep mysql
  1. 安装mysql5.6
yum install mysql-community-server -y
  1. 设置开机启动
chkconfig mysqld on 
  1. 启动mysqld
service mysqld start
  1. 设置root密码(默认密码为空,数据库root账户,密码统一设置为ABCabc123)
mysql_secure_installation
  1. 登录mysql
mysql -uroot -p
  1. 允许root远程登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'ABCabc123' WITH GRANT OPTION;
mysql> flush privileges;
  1. 修改所有编码为utf-8,替换/etc/my.cnf文件
vi /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character-set-server=utf8
collation-server=utf8_general_ci
sql_mode='NO_ENGINE_SUBSTITUTION'

key_buffer=16K
table_open_cache=4
query_cache_limit=256K
query_cache_size=4M
max_allowed_packet=1M
sort_buffer_size=64K
read_buffer_size=256K
thread_stack=64K
innodb_buffer_pool_size = 56M

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

[mysql]
default-character-set = utf8

[mysql.server]
default-character-set = utf8

[client]
default-character-set = utf8

12. 保存后,重启mysqld

```shell
 service mysqld restart
  1. 登录mysql,查看编码
mysql -uroot -p
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
  1. 至此,mysql5.6安装完成
PS:
需要安装DBproxy的前提是:
1. 所有从库的账号密码都要相同

mysql5.6主从库配置

  1. 环境:

统一mysql版本:5.6.41-log MySQL Community Server (GPL)

统一centOs版本:centos-release-7-5.1804.el7.centos.x86_64

主机:
master:192.168.10.86
               
从机:  
slave1:192.168.10.87
slave2:192.168.10.88

PS:本markdown文件中,所有涉及IP地址的操作,应参照真实情况

master配置

  1. 登录mysql
mysql –u root –p
  1. 创建数据库:
create database repl; 
  1. 修改master机器中mysql配置文件/etc/my.cnf
vi /etc/my.cnf
  1. [mysqld]配置段添加如下字段
server-id=1
log-bin=mysql-bin
log-slave-updates=1
binlog-do-db=repl  #需要同步的数据库,如果没有本行表示同步所有的数据库
binlog-ignore-db=mysql  #被忽略的数据
  1. master机上为slave添加一同步帐号 (开放一个账号用于同步)
grant replication slave on *.* to 'repl'@'%' identified by '123456';
flush  privileges;
  1. 重启mastermysqld服务
service mysqld restart

slave配置

  1. 登录mysql
mysql –u root –p
  1. 使用show master status,查看master的状态,记录下FilePosition,如下列结果中的File:mysql-bin.000004;Position:2205,需要注意的是,这两个字段数据不是固定的,所以配置每个slave节点时,都应该查看一次,再根据实际情况配置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |     2205 | repl         | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  1. 修改slave机器中mysql配置文件/etc/my.cnf
vi /etc/my.cnf
  1. 同样在[mysqld]字段下添加如下内容,需要注意的是,每个slaveserver-id都需要不同。
server-id=2 #
log-bin= mysql-bin
relay-log= mysql-relay-bin
read-only=1
log-slave-updates=1
replicate-do-db=repl #要同步的数据库,不写本行表示同步所有数据库
  1. 重启slavemysqld服务
service mysqld restart
  1. slave上验证对master连接
mysql -h192.168.10.86 -urepl -p123456
  1. 设置slave复制 ,这一步中的参数,请根据实际情况填写,其中,MASTER_LOG_FILEMASTER_LOG_POS的值,即为步骤2中记录的值。
    或者也可以使用默认配置MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0,
CHANGE MASTER TO
MASTER_HOST='192.168.10.86',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=0,
MASTER_CONNECT_RETRY=10;
  1. 启动slave
start slave
  1. 运行show slave status\G查看输出结果,主要查看的是如下图所示,Slave_IO_RunningSlave_SQL_Running两列是否都为YES

1536305708394

测试是否能够同步

  1. master上新建表,必须在repl数据库下,(因为我们刚开始只配置了repl的数据库同步 )
mysql> use repl

Database changed

mysql> create table test(id int,name char(10));

Query OK, 0 rows affected (0.00 sec)

mysql> insert into test values(1,'zaq');

Query OK, 1 row affected (0.00 sec)

mysql> insert into test values(1,'xsw');

Query OK, 1 row affected (0.00 sec)

mysql> select * from test;
+------+------+

| id    | name |

+-------+------+

|    1    | zaq   |

|    1    | xsw |

+-------+------+

2 rows in set (0.00 sec)
  1. slave查看是否同步
mysql> use repl;

Database changed

mysql> select * from test;

+------+------+

| id    | name |

+------+------+

|     1 | zaq   |

|     1 | xsw |

+------+------+

2 rows in set (0.00 sec)
  1. 至此,mysql主从库已配置完成

猜你喜欢

转载自blog.csdn.net/lik_lik/article/details/82765698