MySQL主备复制集群安装详细步骤

目录

 

0. 前言

1. 准备与下载

2. 安装MySQL主实例

3. 安装MySQL备实例

4. 主备实例建立连接


0. 前言

先给MySQL打个广告简单介绍一下

MySQL作为一个RDBMS数据库的优势:

  1. 运行速度快,MySQL体积小,命令执行的速度快。
  2. 使用成本低。MySQL是开源的,且提供免费版本,对大多数用户来说大大降低了使用成本。
  3. 使用容易。与其他大型数据库的设置和管理相比,其复杂程度较低,易于使用。
  4. 可移植性强。MySQL能够运行与多种系统平台上,如windouws,Linux,Unix等。
  5. 适用更多用户。MySQL支持最常用的数据管理功能,适用于中小型企业甚至大型网站应用。

MySQL集群的优势:

  1. 99.999%的高可用性
  2. 快速的自动失效切换
  3. 灵活的分布式体系结构,没有单点故障
  4. 高吞吐量和低延迟
  5. 可扩展性强,支持在线扩容

MySQL基础语法介绍见菜鸟网站: https://www.runoob.com/mysql/mysql-tutorial.html

前方预警,干货来了!!!

 

1. 准备与下载

主从集群,顾名思义就是一个master主机,多个slave从属机,为了简便,我们这里只用两台linux服务器做集群当演练,一个当master,一个当slave,后续如需要集群扩容,可以多加slave机器,操作与之相同

登陆MySQL官网,进入社区版https://dev.mysql.com/downloads/mysql/5.7.html#downloads,选择linux 通用版,这里我使用下载好的5.7.19版

MySQL主备复制集群安装详细步骤_第1张图片

把该安装包分别上传到主(192.168.xxx.133),备(192.168.xxx.134) 实例所在的服务器/opt中,需提前安装好jdk

2. 安装MySQL主实例

步骤1. 把下载好的安装包上传到主实例服务器(192.168.xxx.133),并切换到root用户

步骤2. 解压压缩包,并把压缩包改名为mysql

  • [root@localhost opt]#  tar -xvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
  • [root@localhost opt]#  mv mysql-5.7.19-linux-glibc2.12-x86_64 mysql

步骤3. 添加mysql用户和组

  • [root@localhost opt]#  groupadd mysql
  • [root@localhost opt]#  useradd mysql -g mysql

步骤4. 创建mysql目录并修改属组和权限

  • [root@localhost opt]# mkdir -p /opt/mysql/data/assettree-dbsvr
  • [root@localhost opt]# mkdir -p /opt/mysql/tmp
  • [root@localhost opt]# chown -R mysql:mysql /opt/mysql
  • [root@localhost opt]# chmod -R 777 /opt/mysql

步骤5. 执行下面命令初始化数据库

  • [root@localhost opt]# /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data/assettree-dbsvr

    打印下面的信息代表初始化成功,记住密码 qCf,j5bu-UfO

    2019-08-02T02:52:12.927579Z 1 [Note] A temporary password is generated for root@localhost: qCf,j5bu-UfO

步骤6. 检查/opt/mysql/data/assettree-dbsvr下是否有下面文件

    auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  sys

步骤7. 在/opt/mysql/data/assettree-dbsvr下创建my.cnf文件,并改权限为664,权限太大mysql会忽略这个文件,属组改为mysql:mysql

  • [root@localhost assettree-dbsvr]# touch my.cnf
  • [root@localhost assettree-dbsvr]# chown mysql:mysql my.cnf 
  • [root@localhost assettree-dbsvr]# chmod 664 my.cnf 
  • [root@localhost assettree-dbsvr]# vim my.cnf 
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data/assettree-dbsvr
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
log-error = /opt/mysql/data/assettree-dbsvr/mysql.err
pid-file = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.pid
character-set-server=utf8
tmpdir = /opt/mysql/tmp
binlog_format = row
default-storage-engine=INNODB
max_connections=1000
max_allowed_packet = 16M
innodb_buffer_pool_size = 1G
server-id = 100
log-bin = /opt/mysql/data/assettree-dbsvr/mysql_bin
log-bin-index = /opt/mysql/data/assettree-dbsvr/mysql_bin.index
relay-log = /opt/mysql/data/assettree-dbsvr/relay_bin.index
relay-log-index = /opt/mysql/data/assettree-dbsvr/relay_bin.index
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 16M
[client]
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
default-character-set=utf8

 步骤8. 切换到mysql用户,映射my.cnf文件启动mysql,由于会进程一直跑,故可放到后台

  • [root@localhost assettree-dbsvr]# su mysql
  • [mysql@localhost assettree-dbsvr]$ nohup /opt/mysql/bin/mysqld_safe --defaults-file="/opt/mysql/data/assettree-dbsvr/my.cnf" --safe-user-create --basedir=/opt/mysql --user=mysql &

    这里如果需要关闭mysql,可以用 /opt/mysql/bin/mysqladmin -uroot -p -h127.0.0.1  shutdown, 也可以直接杀死进程,但       一般不推荐

步骤9. 切换到root用户,用之前生成的密码登陆mysql

  • [mysql@localhost assettree-dbsvr]$ exit
  • [root@localhost assettree-dbsvr]# /opt/mysql/bin/mysql -uroot -pqCf,j5bu-UfO -h127.0.0.1

步骤10. 执行下面mysql,进行更改密码为root,创建用户和刷新权限

  • mysql> set password=password('root');
  • mysql> grant all privileges on *.* to 'root'@'%' identified by 'root';
  • mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'root';GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;
  • mysql> CREATE USER 'rpluser'@'%' IDENTIFIED BY 'root';GRANT replication slave ON *.* TO 'rpluser'@'%' WITH GRANT OPTION;GRANT ALL PRIVILEGES ON *.* TO 'rpluser'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;RESET MASTER;
  • mysql> CREATE USER 'testbot'@'%' IDENTIFIED BY 'root';GRANT ALL PRIVILEGES ON *.* TO 'testbot'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;

3. 安装MySQL备实例

备实例安装和主实例前七步是一致的

步骤1. 把下载好的安装包上传到主实例服务器(192.168.xxx.134),并切换到root用户

步骤2. 解压压缩包,并把压缩包改名为mysql

  • [root@localhost opt]#  tar -xvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
  • [root@localhost opt]#  mv mysql-5.7.19-linux-glibc2.12-x86_64 mysql

步骤3. 添加mysql用户和组

  • [root@localhost opt]#  groupadd mysql
  • [root@localhost opt]#  useradd mysql -g mysql

步骤4. 创建mysql目录并修改属组和权限

  • [root@localhost opt]# mkdir -p /opt/mysql/data/assettree-dbsvr
  • [root@localhost opt]# mkdir -p /opt/mysql/tmp
  • [root@localhost opt]# chown -R mysql:mysql /opt/mysql
  • [root@localhost opt]# chmod -R 777 /opt/mysql

步骤5. 执行下面命令初始化数据库

  • [root@localhost opt]# /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data/assettree-dbsvr

    打印下面的信息代表初始化成功,记住密码 Q<(.!M5ppwn! 

    2019-08-02T02:52:12.927579Z 1 [Note] A temporary password is generated for root@localhost: Q<(.!M5ppwn!

步骤6. 检查/opt/mysql/data/assettree-dbsvr下是否有下面文件

    auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  sys

步骤7. 在/opt/mysql/data/assettree-dbsvr下创建my.cnf文件,并改权限为664,权限太大mysql会忽略这个文件,属组改为mysql:mysql

  • [root@localhost assettree-dbsvr]# touch my.cnf
  • [root@localhost assettree-dbsvr]# chown mysql:mysql my.cnf 
  • [root@localhost assettree-dbsvr]# chmod 664 my.cnf 
  • [root@localhost assettree-dbsvr]# vim my.cnf 
[mysqld]
basedir = /opt/mysql
datadir = /opt/mysql/data/assettree-dbsvr
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
log-error = /opt/mysql/data/assettree-dbsvr/mysql.err
pid-file = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.pid
character-set-server=utf8
tmpdir = /opt/mysql/tmp
binlog_format = row
default-storage-engine=INNODB
max_connections=1000
max_allowed_packet = 16M
innodb_buffer_pool_size = 1G
server-id = 200  #每一个数据库id需要不同
log-bin = /opt/mysql/data/assettree-dbsvr/mysql_bin
log-bin-index = /opt/mysql/data/assettree-dbsvr/mysql_bin.index
relay-log = /opt/mysql/data/assettree-dbsvr/relay_bin.index
relay-log-index = /opt/mysql/data/assettree-dbsvr/relay_bin.index
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
read_only=1    #需添加
[mysqldump]
quick
max_allowed_packet = 16M
[client]
port = 3306
socket = /opt/mysql/data/assettree-dbsvr/assettree-dbsvr.sock
default-character-set=utf8

步骤8. 切换到mysql用户,切记一定要切到mysql用户,映射my.cnf文件启动mysql,由于会进程一直跑,故可放到后台

  • [root@localhost assettree-dbsvr]# su mysql
  • [mysql@localhost assettree-dbsvr]$ /opt/mysql/bin/mysqld_safe --defaults-file="/opt/mysql/data/assettree-dbsvr/my.cnf" --safe-user-create --basedir=/opt/mysql --user=mysql &

    这里如果需要关闭mysql,可以用 /opt/mysql/bin/mysqladmin -uroot -p -h127.0.0.1  shutdown, 也可以直接杀死进程,但       一般不推荐

步骤9. 切换到root用户,用之前生成的密码登陆mysql

  • [mysql@localhost assettree-dbsvr]$ exit
  • [root@localhost assettree-dbsvr]# /opt/mysql/bin/mysql -uroot -pqCf,j5bu-UfO -h127.0.0.1

步骤10. 执行下面mysql,进行更改密码为root,创建用户和刷新权限

  • mysql> set password=password('root');
  • mysql> grant all privileges on *.* to 'root'@'%' identified by 'root';
  • mysql> CREATE USER 'admin'@'%' IDENTIFIED BY 'root';GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;
  • mysql> CREATE USER 'rpluser'@'%' IDENTIFIED BY 'root';GRANT replication slave ON *.* TO 'rpluser'@'%' WITH GRANT OPTION;GRANT ALL PRIVILEGES ON *.* TO 'rpluser'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;RESET MASTER;
  • mysql> CREATE USER 'testbot'@'%' IDENTIFIED BY 'root';GRANT ALL PRIVILEGES ON *.* TO 'testbot'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;

4. 主备实例建立连接

步骤1. 分别以管理员身份登陆主备数据库 

  • [root@localhost assettree-dbsvr]# /opt/mysql/bin/mysql -uroot -proot -h127.0.0.1

步骤2. 在主数据库查看master 状态,记录file和position,下面会用

  • mysql> show master status;

       +------------------+----------+--------------+------------------+-------------------+
       | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
       +------------------+----------+--------------+------------------+-------------------+
       | mysql_bin.000001 |      768 |              |                  |                   |
       +------------------+----------+--------------+------------------+-------------------+
       1 row in set (0.00 sec)

步骤3. 在备数据库执行命令,建立主备连接,并开启slave,查看slave状态

  • mysql> change master to MASTER_HOST='192.168.xxx.133',  MASTER_PORT=3306, master_user='rpluser', master_password='root',MASTER_LOG_FILE='mysql_bin.000001',MASTER_LOG_POS=768;
  • mysql> start slave;
  • mysql> show slave status\G

    在展开的日志中找到

            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

    即代表成功!

    可进行测试主备集群是否搭建成功,如果某个数据库DDL,DML操作等都能在其他数据库里立马体现出来,则证明真的成功        了!

          MySQL主备复制集群安装详细步骤_第2张图片

 

 

你可能感兴趣的:(mysql)