我把文档拆分成多个部分,然后各自组合成了全新安装和现有环境升级安装,各位可以根据自己的情况选择
全新安装
安装步骤:1.master1节点配置—》2.master2节点配置—》3.主从配置
现有环境升级安装
安装步骤:4.数据备份—》2.master2节点配置—》5.数据恢复—》6.卸载mysql5.6环境——》1.master1节点配置—》5.数据恢复—》3.主从配置
1.master1节点配置
创建目录
cd /home
mkdir mysql
cd /home/mysql
mkdir ./{binlog,data,undo,redo,errorlog,slowlog,sock,script}
解压文件
将mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz上传到服务器/home目录下
cd /home/mysql
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql-5.7.27
添加mysql用户及用户组
groupadd mysql
useradd -r -g mysql mysql
修改当前目录拥有者为mysql用户
chown -R mysql:mysql /home/mysql
修改配置文件 将下面配置添加到 配置文件中
vim /etc/my.cnf
[client]
socket=/home/mysql/sock/mysql.sock
[mysqld]
basedir=/home/mysql/mysql-5.7.27
datadir=/home/mysql/data
socket=/home/mysql/sock/mysql.sock
port=3307
server-id = 1
lc-messages-dir=/home/mysql/mysql-5.7.27/share
innodb_data_home_dir = /home/mysql/data
innodb_data_file_path = ibdata1:50M:autoextend
innodb_file_per_table #支持私有表空间
innodb_undo_tablespaces = 3
innodb_undo_logs = 128
innodb_undo_directory = /home/mysql/undo/
event_scheduler = ON #支持触发器
log-bin = /home/mysql/binlog/mysql-bin
max_binlog_size = 500M
binlog_format = row
gtid_mode = on
enforce-gtid-consistency = on
sync_binlog = 1
slave-skip-errors = all
master_info_repository=TABLE
auto-increment-increment = 2
auto-increment-offset = 1
symbolic-links=0
lower_case_table_names=1
transaction-isolation=read-committed
expire_logs_days = 10
innodb_buffer_pool_size=524288000
skip-name-resolve
max_connections = 1000
max_connect_errors = 6000
[mysqld_safe]
log-error=/home/mysql/errorlog/mysqld.log
pid-file=/home/mysql/mysqld.pid
安装数据库
cd /home/mysql/mysql-5.7.27/bin
./mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize-insecure
修改启动文件
cd /home/mysql/mysql-5.7.27/support-files
vim mysql.server
basedir=/home/mysql/mysql-5.7.27
datadir=/home/mysql/data
MySQL启动
./mysql.server start
登录mysql修改权限,直接回车,默认空密码
cd /home/mysql/mysql-5.7.27/bin
./mysql -uroot -p
执行sql命令
mysql> alter user 'root'@'localhost' identified by '!qaz@wsx';
mysql>flush privileges;
mysql>grant all privileges on . to root@'%' identified by '!qaz@wsx';
mysql>flush privileges;
mysql> grant replication slave,replication client on . to repl@'%' identified by '!qaz@wsx';
mysql>exit;
配置环境变量
将下面内容加到/etc/profile文件最后的位置
vim /etc/profile
export MYSQL_HOME=/home/mysql/mysql-5.7.27
export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile
设置开机启动
cp /home/mysql/mysql-5.7.27/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
service mysqld status
2.Master2节点配置
创建目录
cd /home
mkdir mysql
cd /home/mysql
mkdir ./{binlog,data,undo,redo,errorlog,slowlog,sock,script}
解压文件
将mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz上传到服务器/home目录下
cd /home/mysql
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql-5.7.27
添加mysql用户及用户组
groupadd mysql
useradd -r -g mysql mysql
修改当前目录拥有者为mysql用户
chown -R mysql:mysql /home/mysql
修改配置文件 将下面配置添加到 配置文件中
vim /etc/my.cnf
[client]
socket=/home/mysql/sock/mysql.sock
[mysqld]
basedir=/home/mysql/mysql-5.7.27
datadir=/home/mysql/data
socket=/home/mysql/sock/mysql.sock
port=3307
server-id = 2
lc-messages-dir=/home/mysql/mysql-5.7.27/share
innodb_data_home_dir = /home/mysql/data
innodb_data_file_path = ibdata1:50M:autoextend
innodb_file_per_table #支持私有表空间
innodb_undo_tablespaces = 3
innodb_undo_logs = 128
innodb_undo_directory = /home/mysql/undo/
event_scheduler = ON
log-bin = /home/mysql/binlog/mysql-bin
max_binlog_size = 500M
binlog_format = row
gtid_mode = on
enforce-gtid-consistency = on
sync_binlog = 1
slave-skip-errors = all
master_info_repository=TABLE
auto-increment-increment = 2
auto-increment-offset = 2
symbolic-links=0
lower_case_table_names=1
transaction-isolation=read-committed
expire_logs_days = 10
innodb_buffer_pool_size=524288000
skip-name-resolve
max_connections = 1000
max_connect_errors = 6000
[mysqld_safe]
log-error=/home/mysql/errorlog/mysqld.log
pid-file=/home/mysql/mysqld.pid
安装数据库
cd /home/mysql/mysql-5.7.27/bin
./mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize-insecure
修改启动文件
cd /home/mysql/mysql-5.7.27/support-files
vim mysql.server
basedir=/home/mysql/mysql-5.7.27
datadir=/home/mysql/data
MySQL启动
./mysql.server start
登录mysql修改权限,直接回车,默认空密码
cd /home/mysql/mysql-5.7.27/bin
./mysql -uroot -p
执行sql命令
mysql> alter user 'root'@'localhost' identified by '!qaz@wsx';
mysql>flush privileges;
mysql>grant all privileges on . to root@'%' identified by '!qaz@wsx';
mysql>flush privileges;
mysql> grant replication slave,replication client on . to repl@'%' identified by '!qaz@wsx';
mysql>flush privileges;
mysql>exit;
配置环境变量
将下面内容加到/etc/profile文件最后的位置
vim /etc/profile
export MYSQL_HOME=/home/mysql/mysql-5.7.27
export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile
设置开机启动
cp /home/mysql/mysql-5.7.27/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld restart
service mysqld status
3.主从配置
Master1节点数据库登录
mysql -uroot -p
执行sql命令
mysql> change master to master_host='master2节点ip',master_user='repl',master_password='!qaz@wsx',master_port=3307,master_auto_position=1;
mysql>start slave;
mysql>show slave status\G;
查看主要参数
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql>exit;
Master2节点数据库登录
mysql -uroot -p
执行sql命令
mysql> change master to master_host='master1节点ip',master_user='repl',master_password='!qaz@wsx',master_port=3307,master_auto_position=1;
mysql>start slave;
mysql>show slave status\G;
查看主要参数
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
mysql>exit;
4.数据备份
登录现有mysql环境服务器
mysqldump -uroot -p –all-databases > /home/all.sql
5.数据恢复
master2节点恢复数据
将备份的all.sql文件上传到master2节点/home/mysql目录下
mysql -uroot –p
mysql>source /home/mysql/all.sql
mysql>exit;
6.卸载mysql5.6环境
登录现有mysql环境服务器
service mysqld stop
rm -rf /usr/local/mysql
rm -rf /etc/my.cnf