MySQL5.7安装及主从配置

MySQL5.7安装及主从配置

2021年9月

目 录
MySQL5.7安装及主从配置 1
1 文档介绍 3
1.1 文档目的 3
1.2 文档范围 3
1.3 读者对象 3
1.4 参考文献 3
1.5 术语与缩写解释 3
2 安装MySQL 3
2.1 操作系统磁盘分区及目录说明 3
2.2 基础环境准备 4
2.2.1 禁止iptables 4
2.2.2 禁止 selinux 4
2.3 安装MySQL 5
2.3.1 下载MySQL安装包 5
2.3.2 上传MySQL安装包 5
2.3.3 添加组和用户 5
2.3.4 解压MySQL安装包 6
2.3.5 创建软链接 6
2.3.6 创建基本目录 6
2.3.7 创建/etc/my.cnf配置文件 6
2.3.8 数据库初始化 7
3 MySQL主从配置 10
mysql主从同步配置 10
4 测试MySQL主从配置 19

1 文档介绍
1.1 文档目的
文档主要描述MySQL5.7安装,这里以操作系统REHAT7.7为例,数据库版本5.7.27为例。
1.2 文档范围

1.3 读者对象
项目实施人员、安装与维护人员等。
1.4 参考文献
1.5 术语与缩写解释
缩写、术语 解 释

2 安装MySQL
2.1 操作系统磁盘分区及目录说明
目录 用处
/ Linux系统文件及一些常用软件放置,生产环境建议把/var、/opt、/home可以考虑单独独立出来。
/u01/mysql/mysql 数据库安装位置
/u01/mysql/mysql/data 数据库保存位置, (对于压力大数据库建议单独划分)
/u01/mysql/mysql/log 日志保存位置, 用于存储mysql的binlog日志文件。(对于压力大数据库建议单独划分)
说明:swap不要划分,对于频繁进行读写操作的OLTP系统而言,响应时间过长很可能会直接拖垮整个系统。
对于数据库压力比较大的服务器,可以在考虑将数据文件与日志文件分开存储。也就是在安装操作系统时,单独划分/logs磁盘空间。
scp /u01/upload/mysql-5.7.27-el7-x86_64.tar.gz root@192.168.92.14:/u01/upload/
从一台服务器上将mysql安装包给复制到另一台服务器上去
[root@i-e962au73 upload]# scp /u01/upload/mysql-5.7.27-el7-x86_64.tar.gz root@192.168.92.14:/u01/upload/
Warning: Permanently added ‘192.168.92.14’ (ECDSA) to the list of known hosts.
root@192.168.92.14’s password:
mysql-5.7.27-el7-x86_64.tar.gz 100% 681MB 180.3MB/s 00:03
2.2 基础环境准备
服务器 端口
192.168.92.13 3313
192.168.92.14 3313
2.2.1 禁止iptables
[root@MySQL-Node1 ~]# /etc/init.d/iptables stop
[root@MySQL-Node1 ~]# chkconfig --list|grep iptables
[root@MySQL-Node1 ~]#

2.2.2 禁止 selinux
[root@MySQL-Node1 ~]# getenforce 0
[root@MySQL-Node1 ~]# vim /etc/sysconfig/selinux
把SELINUX=enforcing 替换为:
SELINUX=disabled

2.3 安装MySQL
2.3.1 下载MySQL安装包

2.3.2 上传MySQL安装包
将MySQL安装包mysql-5.7.27-el7-x86_64.tar.gz上传至/u01/upload目录下。
[root@MySQL-Node1 ~]# cd /u01/upload/
[root@MySQL-Node1 software]# ls
mysql-5.7.27-el7-x86_64.tar.gz
2.3.3 添加组和用户
[root@i-jzp7arkt upload]# groupadd mysql
[root@i-jzp7arkt upload]# useradd -g mysql -s /sbin/nologin -d /u01/mysql/mysql mysql
[root@i-jzp7arkt upload]# id mysql
uid=1000(mysql) gid=1000(mysql) groups=1000(mysql)
说明:/u01/mysql/mysql为MySQL软件安装目录
2.3.4 解压MySQL安装包
将MySQL安装包解压至/u01/mysql/mysql目录
[root@MySQL-Node1 ~]#mkdir /u01/mysql/mysql
[root@MySQL-Node1 ~]# cd /u01/mysql/mysql
[root@MySQL-Node1 mysql]# tar -zxvf /u01/upload/mysql-5.7.27-el7-x86_64.tar.gz
[root@i-jzp7arkt upload]# tar -zxvf mysql-5.7.27-el7-x86_64.tar.gz(与上面一样目的 解压)
[root@MySQL-Node1 mysql]# ls -lh
total 4.0K
drwxr-xr-x 9 7161 31415 4.0K Jul 12 21:03 mysql-5.7.27-el7-x86_64
[root@MySQL-Node1 mysql]# du -sh mysql-5.7.27-el7-x86_64/
2.5G mysql-5.7.27-el7-x86_64/ //MySQL安装目录大小为2.5G
2.3.5 创建软链接
[root@MySQL-Node1 mysql]# mv /u01/mysql/mysql/mysql-5.7.27-el7-x86_64/* /u01/mysql/mysql
[root@MySQL-Node1 mysql]# cd /usr/local/
[root@MySQL-Node1 local]# ln -s /u01/mysql/mysql mysql
2.3.6 创建基本目录
[root@i-jzp7arkt local]# mkdir -p /u01/mysql/mysql/data
[root@i-jzp7arkt local]# mkdir -p /u01/mysql/mysql/log
[root@i-jzp7arkt local]# mkdir -p /u01/mysql/mysql/tmp
[root@MySQL-Node1 mysql3306]# chown -R mysql:mysql /u01/mysql/mysql

2.3.7 创建/etc/my.cnf配置文件
直接在/etc目录下替换或者新建my.cnf文件。内容见附件
[root@i-jzp7arkt etc]# pwd
/etc
[root@i-jzp7arkt etc]# cp my.cnf my.cnf.bak202109281501 这个是最新的。。。注意
2.3.7.1 添加环境变量
[root@MySQL-Node1 mysql]# echo P A T H e c h o " e x p o r t P A T H = PATH echo "export PATH= PATHecho"exportPATH=PATH:/u01/mysql/mysql/bin">> /etc/profile
source /etc/profile
2.3.8 数据库初始化
初始化命令:
[root@MySQL-Node1 ~]# /u01/mysql/mysql/bin/mysqld –initialize 正常执行这个就行
如果该步骤执行报错的话
[root@i-jzp7arkt bin]# mysqld --initialize explicit_defaults_for_timestamp=true 补充说明
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法:[root@example.com data]# yum install -y libaio //安装后在初始化就OK了,但是初始化的时候也是需要注意的,要注意位数 这个才对
[root@i-jzp7arkt log]# yum install numactl.x86_64
注意:5.7版本初始化后会得到密码(在error.log日志中找到):PYtcUiQvx6)/ 必须记住此密码,用此登录后做权限更改操作。
[root@MySQL-Node1 logs]# cat error.log |grep password
2016-12-27T01:00:31.224648Z 1 [Note] A temporary password is generated for root@localhost: PYtcUiQvx6)/
[root@i-jzp7arkt log]# cat mysql-error.log |grep password
2021-09-28T08:22:16.272768Z 1 [Note] A temporary password is generated for root@localhost: ,sJ:x12alt=i
2.3.8.1 创建启动脚本
[root@i-jzp7arkt log]# cd /u01/mysql/mysql/
[root@i-jzp7arkt mysql]# cp support-files/mysql.server /etc/init.d/mysql
2.3.8.2 启动MySQL服务
[root@i-jzp7arkt log]# /etc/init.d/mysql start
Starting MySQL. SUCCESS! [ OK ]
或者也可以这种方式启动:
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf &

2.3.8.3 登录MySQL并更改密码
[root@i-jzp7arkt log]# mysql -S /u01/mysql/mysql/mysql.sock -p
Logging to file ‘/data/local/mysql/log/query.log’
Enter password: —输入前面的密码PYtcUiQvx6 lMzb-ZVap6f2
注意:成功登录后执行命令提示如下。
(unknown)@localhost:mysql.sock 09:18:40 [(none)]>
show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. 登录无操作权限,前面的提示符是“(unknown)”
2.3.8.4 更改MySQL数据库root用户密码
(unknown)@localhost:mysql.sock 09:22:48 [(none)]>alter user user() identified by ‘user’;
Query OK, 0 rows affected (0.00 sec)
alter user ‘root’@‘localhost’ identified by ‘fuhai1998’;

root@localhost:mysql.sock 09:24:51 [(none)]>—修改密码后,前面的提示符由“ unknown”修改为“root”
root@localhost:mysql.sock 09:24:51 [(none)]>show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+
4 rows in set (0.00 sec)

root@localhost:mysql.sock 09:50:39 [(none)]>
select user,host,authentication_string,plugin from mysql.user;
注意:mysql5.7mysql.user中无 “password” 字段;plugin 为“ mysql_native_password” 表示采用本地的加密认证,也可以采用其它加密方式,
如集中认证等+
-----------±----------±------------------------------------------±----------------------+
| user | host | authentication_string | plugin |
±----------±----------±------------------------------------------±----------------------+
| root | localhost | *52387EA2A80926B64E3F988ED7B1585AB22F5C14 | mysql_native_password |
| mysql.sys | localhost | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password |
±----------±----------±------------------------------------------±----------------------+
2 rows in set (0.00 sec)

2.3.8.5 设置root远程连接(注:生产环境不建议设置)
“root@localhost Mon Aug 17 16:32:39 2015 16:32:39 [(none)]> grant all privileges on . to root@”%" identified by ‘user’ with grant option;
"root@localhost Mon Aug 17 16:32:47 2015 16:32:47 [(none)]>flush privileges;
"root@localhost Mon Aug 17 16:33:02 2015 16:33:02 [(none)]>exit

2.3.8.6 MySQL用户账户管理及备份

对用户严格管理,单用户只能赋予执行单个业务的权限,防止对数据的误操作。示范命令如下:

创建用户:
Mysql> grant select,insert,update,delete on csf_cms.* to ‘csf_cms_admin’@‘192.168.0.97’ identified by ‘cca0810012045’;

查看用户:

MySQL> Select * from mysql.user;

Mysql> Select * from mysql.db;

Mysql> select * from mysql. tables_priv;

Mysql> select * from mysql. columns_priv;

Mysql> select * from mysql. procs_priv;

赋予权限

Mysql> grant update on csf_maec.* to ‘jerry.kang’@‘192.168.0.103’;

Mysql> grant update(flag) on csf_maec.ms_company to ‘jerry.kang’@‘192.168.0.103’;

收回权限

revoke drop on . from ‘sun.shang’@‘192.168.0.99’;

删除过期用户

mysql> drop user ‘glpiuser’@‘192.168.0.25’;

启动命令:su - mysql -c “/u01/mysql/mysql/mysqld start”
关闭命令su - mysql -c “/u01/mysql/mysql/mysqld stop”
主从服务器配置搭建
3 MySQL主从配置
mysql主从同步配置
Mysql的主从复制至少是需要两个Mysql的服务,当然Mysql的服务是可以分布在不同的服务器上,也可以在一台服务器上启动多个服务。
首先确保主从服务器上的Mysql版本相同。
[root@i-e962au73 ~]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile

  • base: mirrors.tuna.tsinghua.edu.cn
  • extras: mirrors.tuna.tsinghua.edu.cn
  • updates: mirrors.aliyun.com
    base | 3.6 kB 00:00:00
    extras | 2.9 kB 00:00:00
    updates | 2.9 kB 00:00:00
    No package mysql-server available.
    下载server报错
    [root@i-e962au73 ~]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
    [root@i-e962au73 ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
    安装服务器端:yum install mysql-server
           yum install mysql-devel
    安装客户端: yum install mysql
    二、配置mysql主从同步
    准备两台测试的虚拟机,如上安装mysql环境,并开启mysql服务
    主master : 192.168.92.13
    从slave : 192.168.92.14
    我们看出,两个服务器的数据库是一致的,这个时候,我们在两个数据库服务器建立一个空的test数据库。 create database yunweia;.在运维的时候,需要锁定mysql的写入use database yunweia;FLUSH TABLES WITH READ LOCK;
    1、 配置主库:create database yunweia;#建立数据库yunweia
    1)、授权给从数据库服务器
    创建从库同步数据的账号:
    mysql> GRANT REPLICATION SLAVE ON . to ‘rep1’@‘192.168.92.14’ identified by ‘test123456’;
    Query OK, 0 rows affected, 1 warning (0.00 sec)
    mysql> FLUSH PRIVILEGES;
    2)、修改主库配置文件,开启binlog,并设置server-id,每次修改配置文件后都要重启mysql服务才会生效
    vim /etc/my.cnf 在该配置文件[mysqld]下面添加下面内容:
    #@addby fuhai_fzy 20210929 begin
    server-id = 1
    #log-bin=/u01/mysql/mysql/binlog
    log-bin=master-bin
    #binlog_formmar=ROW
    #binlog_row_image=minimal
    binlog-do-db = yunweia
    #@addby fuhai_fzy 20210929
    注解:
    server-id:master端的ID号;
    log-bin:同步的日志路径及文件名,一定注意这个目录要是mysql有权限写入的(我这里是偷懒了,直接放在了下面那个datadir下面);
    binlog-do-db:要同步的数据库名
    还可以显示 设置不同步的数据库:
    binlog-ignore-db = mysql 不同步mysql库和test库
    binlog-ignore-db = test
    修改配置文件后,重启服务:service mysql restart
    如果启动失败,通过cat /var/log/mysqld.log | tail -30 查看mysql启动失败的日志,从日志内容寻找解决方案。
    3)、查看主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复
    root@i-e962au73 binlog]# mysql -uroot -pfuhai1998
    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 2
    Server version: 5.7.27-log MySQL Community Server (GPL)
    Copyright © 2000, 2021, 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 |
    ±--------------±---------±-------------±-----------------±------------------+
    | binlog.000001 | 154 | yunweia | | |
    ±--------------±---------±-------------±-----------------±------------------+
    1 row in set (0.00 sec)
    mysql> show master status\G;
    *************************** 1. row ***************************
    File: binlog.000004
    Position: 154
    Binlog_Do_DB: yunweia
    Binlog_Ignore_DB:
    Executed_Gtid_Set:
    主服务器已配置好。
    2、配置从库
    1)、理所当然也是从配置文件着手,在/etc/my.cnf 添加下面配置:
    [mysqld]
    #@addby fuhai_fzy 20210929 begin
    #server-id = 1
    server-id = 2
    replicate-do-db = yunweia
    #log_bin=/u01/mysql/mysql/binlog
    log_bin= master-bin
    log-slave-updates
    slave-skip-errors=all
    slave-net-timeout=60
    #@addby fuhai_fzy 20210929
    [mysqld]
    2)、重启mysql服务:
    [root@i-e962au73 ~]# service mysql restart
    ERROR! MySQL server PID file could not be found!
    Starting MySQL. SUCCESS!

设置主服务器ip,同步账号密码,同步位置

3)、执行同步命令
mysql> change master to master_host=‘192.168.92.13’,
-> master_user=‘rep1’,
-> master_password=‘test123456’,
-> master_log_file=‘binlog.000004’,
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

开启同步功能

mysql> start slave;
4)、查看从库的状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 192.168.92.14
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 154
Relay_Log_File: i-jzp7arkt-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB: yunweia
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: 154
Relay_Log_Space: 154
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 2003
Last_IO_Error: error connecting to master ‘rep1@192.168.92.14:3306’ - retry-time: 60 retries: 2
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /u01/mysql/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: 210929 14:12:39
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)
ERROR:
No query specified
上面是报错的 状态两个地方应该都为YES才对
mysql> change master to master_host=‘192.168.92.13’,
-> master_user=‘rep1’,
-> master_password=‘test123456’,
-> master_log_file=‘binlog.000004’,
-> master_log_pos=154;
ERROR 3021 (HY000): This operation cannot be performed with a running slave io thread; run STOP SLAVE IO_THREAD FOR CHANNEL ‘’ first.
关闭io
mysql> STOP SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
重新修改
mysql> change master to master_host=‘192.168.92.13’,
-> master_user=‘rep1’,
-> master_password=‘test123456’,
-> master_log_file=‘binlog.000004’,
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
开启同步功能
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> start SLAVE IO_THREAD; #开启io线程
Query OK, 0 rows affected, 1 warning (0.00 sec)
还是报错 知道脚本是这样了才对。
mysql> stop SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
CHANGE MASTER TO MASTER_HOST=‘192.168.92.13’,MASTER_PORT=3313,MASTER_USER=‘rep1’,MASTER_PASSWORD=‘test123456’,MASTER_LOG_FILE=‘master-bin.000002’,MASTER_LOG_POS=154;
mysql> change master to master_host=‘192.168.92.13’,
-> master_user=‘rep1’,
-> master_password=‘test123456’,
-> master_log_file=‘binlog.000004’,
-> master_port=3313,
-> master_log_pos=154;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start SLAVE IO_THREAD;
Query OK, 0 rows affected (0.00 sec)
mysql> start SLAVE ;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status\G;

至此可算是对了。。。。
4 测试MySQL主从配置
(1)、在主库上创建yunweia模式,即:
create schema yunweia; 可以用这种方式
create database yunweia; 两者其实差不多意思
(2)、在主库上的yunweia模式里面创建comm_config表,即:
use yunweia;
CREATE TABLE comm_config (configId varchar(200) NOT NULL ,configValue varchar(1024) DEFAULT NULL ,description varchar(2000) DEFAULT NULL ,PRIMARY KEY (configId)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
(3)、在主库上往comm_config表中插入一条记录,即:

insert into comm_config(configId, configValue, description) values(‘name’, ‘架构与我’, ‘测试一下’);
insert into comm_config(configId, configValue, description) values(‘FZY’, ‘只爱婷婷’, ‘测试一’);

脚本如下:

(4)、在从库上查看模式,即:
show databases;
(5)、在从库上查看yunweia模式下的表及数据,即:
use yunweia;
show tables;
结果为:
±------------------+
| Tables_in_yunweia |
±------------------+
| comm_config |
±------------------+
1 row in set (0.00 sec)
说明表也同步好了,再查看数据,即:
select * from comm_config;
结果为:mysql> select * from comm_config;
±---------±-------------±----------------+
| configId | configValue | description |
±---------±-------------±----------------+
| FZY | 只爱婷婷 | 只爱巩婷婷 |
| name | 架构与我 | 测试一下 |
| name111 | 架构与我 | 测试两下 |
±---------±-------------±----------------+
说明数据也同步过来了。
5 今天早晨突然发现数据不同步了,查看状态的发现是Slave_IO_Running: Yes Slave_SQL_Running: N

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值