centos7安装mysql5.7并配置主从

一、mysql基础安装

1、系统约定:
安装文件下载目录:/data/software
Mysql目录安装位置:/usr/local/mysql
数据库保存位置:/data/mysql
日志保存位置:/data/log/mysql

2、列出所有被安装的rpm package
rpm -qa | grep mariadb
3、强制卸载
rpm -e --nodeps mariadb-libs-5.5.37-1.el7_0.x86_64 //后面mariadb,根据查到的名称修改
4、删除相关文件夹
find / -name mysql
rm -rf /usr/lib64/mysql
rm -rf /etc/selinux/targeted/active/modules/100/mysql

##如果有安装文件,需要通过rz上传

##如果没有安装文件,可以下载
mkdir /data
mkdir /data/software
mkdir /data/mysql
mkdir /data/log
mkdir /data/log/mysql

cd /data/software
wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz

如果已经有包:

tar -zxvf /opt/tools/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz -C /usr/local

mv mysql-5.7.19-linux-glibc2.12-x86_64/ mysql

5、新建mysql用户、组及目录
##新建一个msyql组
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql
##新建msyql用户禁止登录shell

6、改变目录属有者
cd /usr/local/mysql
pwd
chown mysql.mysql mysql -R
chown -R mysql /data/mysql

7、配置参数
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

########################################################################################################################

##记录生成的临时密码(非常重要,后续要用到)
root@localhost: 0jmcbkfLt2-1

8、修改系统配置文件

cd support-files
cp mysql.server /etc/init.d/mysqld

vim /etc/my.cnf (主服务器,如果只部署一台,就忽略从服务器的配置)

[client]
port=3306
#socket=/usr/local/mysql/mysql.sock
socket=/data/mysql/mysql.sock

[mysqld]
user = mysql
innodb_buffer_pool_size = 128M
log_bin = master-log
max_binlog_size = 64M
binlog_format = mixed
log-error=/var/log/mysql.log
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
pid-file=/tmp/mysqld.pid
server_id = 1

vim /etc/my.cnf (从服务器)

[client]
port=3306
#socket=/usr/local/mysql/mysql.sock
socket=/data/mysql/mysql.sock

[mysqld]
user = mysql
innodb_buffer_pool_size = 128M
log_bin = slave-log
max_binlog_size = 64M
log_slave_updates = on
binlog_format = mixed
relay_log = relay-bin
log-error=/var/log/mysql.log
basedir=/usr/local/mysql
datadir=/data/mysql
socket=/data/mysql/mysql.sock
pid-file=/tmp/mysqld.pid
server_id = 2

vim /etc/profile

找到最后一行:输入:export PATH=/usr/local/mysql/bin:$PATH

source /etc/profile

chown -R mysql:mysql /data/mysql
chmod -R 755 /data/mysql

cd /usr/local/mysql

9、启动mysql
systemctl restart mysqld.service

查看状态:
systemctl status mysqld

自动启动:
chmod 755 /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

10、修改root密码,赋权

mysql -u root -p
–输入第6步生成的临时密码

修改密码:
set password=password(‘新密码’);

创建其他用户:
create user ‘ddweb’@‘localhost’ identified by ‘AfHg5dUufh6%2’;

赋权:所有库.表,给指定的ddweb用户,并只允许以下5个IP进行访问
grant all privileges on . to ‘ddweb’@‘172.16.24.34’ identified by ‘AfHg5dUufh6%2’;
grant all privileges on . to ‘ddweb’@‘172.16.24.35’ identified by ‘AfHg5dUufh6%2’;
grant all privileges on . to ‘ddweb’@‘172.16.24.36’ identified by ‘AfHg5dUufh6%2’;
grant all privileges on . to ‘ddweb’@‘172.16.24.37’ identified by ‘AfHg5dUufh6%2’;
grant all privileges on . to ‘ddweb’@‘172.16.24.38’ identified by ‘AfHg5dUufh6%2’;

刷新权限:flush privileges;

查看权限:
use mysql;
select host,user from user;

二、MySQL主从复制
1、场景描述:
主数据库服务器:172.16.24.36,MySQL已经安装,并且无应用数据。
从数据库服务器:172.16.24.37,MySQL已经安装,并且无应用数据。

》》》在主库36上执行:

授权给从数据库服务器:
GRANT REPLICATION SLAVE ON . to ‘ddweb’@‘172.16.24.37’ identified by ‘AfHg5dUufh6%2’;

查询主数据库状态

Mysql> show master status;

±-----------------±---------±-------------±-----------------+
|
File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

±-----------------±---------±-------------±-----------------+
|
mysql-bin.000005 | 261 | | |

±-----------------±---------±-------------±-----------------+

记录下 FILE 及 Position 的值,在后面进行从服务器操作的时候需要用到

》》》在从库37上执行:
执行同步SQL语句(注:mysql-bin.000005、261就是上述主库查到的信息)
change master to master_host=‘172.16.24.36’,master_user=‘ddweb’,master_password=‘AfHg5dUufh6%2’,master_log_file=‘mysql-bin.000005’,master_log_pos=261,master_port=3306;

正确执行后启动Slave同步进程
mysql> start slave;

主从同步检查
mysql> show slave status

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.24.36
Master_User: ddweb
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1180
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: 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: 1180
Relay_Log_Space: 1713
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
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: 36
Master_UUID: 94f0b88c-dccc-11e8-9fe4-0cda411d7299
Master_Info_File: /data/mysql/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)

其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常

2、验证主从复制效果

》》》主服务器上的操作

在主服务器上创建数据库first_db

mysql> create database first_db;

Query Ok, 1 row affected (0.01 sec)

在主服务器上创建表first_tb

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

Query Ok, 1 row affected (0.00 sec)

在主服务器上的表first_tb中插入记录

mysql> insert into first_tb values (001,’myself’);

Query Ok, 1 row affected (0.00 sec)

》》》在从服务器上查看

show databases;
use first_db;
select * from first_db;
由此,整个MySQL主从复制的过程就完成了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值