mysql5.7主主同步安装
作者:互联网
主主同步事实上就是在主从的基础上,将原先的从机当主机,主机当从机再配置一遍主从同步,两台mysql都可读写,互为主备。
mysql5.7版本主主同步部署
root 下
useradd mysql # 加用户 echo 'root123'|passwd --stdin mysql # 设置密码 tar -zxf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ # 解压 mv mysql-5.6.34-linux-glibc2.5-x86_64 mysql # 重命名 cd /usr/local/mysql #mysql5.7没有data目录,手动创建 mkdir data chown -R mysql:mysql mysql/ # 赋予权限
初始化
# 切换用户 su - mysql cd /usr/local/mysql # 初始化 # mysql5.7和之前版本不同,很多资料上都是这个命令:../scripts/mysql_install_db --user=mysql,而mysql5.7的mysql_install_db命令是在bin目录下 的并且建议 用 mysqld --initialize命令 bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
如下:
bash-4.1$ bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data 2020-09-24T17:18:06.303755Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2020-09-24T17:18:06.303877Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000) 2020-09-24T17:18:06.304157Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-09-24T17:18:07.357897Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-09-24T17:18:07.649634Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-09-24T17:18:07.798293Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: e9febf85-fe89-11ea-b247-000c2932f416. 2020-09-24T17:18:07.814479Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-09-24T17:18:07.842511Z 1 [Note] A temporary password is generated for root@localhost: =;pWEA#kr1zi
注意最后一行,这也是和之有版本不同的地方,它给了root一个初始密码,后面要登录的时候要用到这个密码。
切换用户到root su - root # 备份原来的my.cnf文件 mv my.cnf my.cnf.bak
#复制配置文件和启动文件到/etc/下
cd /usr/local/mysql/support-files #复制MySQL的配置文件 cp my-default.cnf /etc/my.cnf #复制启动文件到系统服务下,可以以服务启动 cp mysql.server /etc/init.d/mysql
#配置启动文件
vim /etc/init.d/mysql #添加以下两行 (文件里正好有 basedir= datadir= 这样的两行,直接填上就可以了) basedir=/usr/local/mysql datadir=/usr/local/mysql/data
# 配置环境变量
vi /etc/profile #添加以下两行 export MYSQL_HOME=/usr/local/mysql export PATH=$MYSQL_HOME/bin:$PATH # 环境变量立即生效 source /etc/profile
#配置mysql服务开机自动启动
chkconfig --add mysql chkconfig mysql on
启动mysql
service mysql start
#登录MySQL
mysql -uroot -p
> 输入初始化最后一行中的密码就可以登录
#登录后,第一次修改密码必须用alter语句
alter user 'root'@'localhost' identified by 'root123' PASSWORD EXPIRE NEVER account unlock;
#设置登录权限(使得root用户可以从任何机器上登录数据库)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root123' WITH GRANT OPTION; #立即生效 flush privileges;
配置my.cnf配置文件
vi /etc/my.cnf #修改忽略大小写 lower_case_table_names=1 #[mysqld]下 # 修改连接数 max_connections=1000 #[mysqld]下
#重启MySQL
service mysql restart
#登录数据库后验证连接数修改完成
show variables like '%max_connections%';
#关闭防火墙
service iptables stop
#创建数据库
create database `auto1` default character set utf8 collate utf8_general_ci;
*******************************************************************以上在两台数据库服务器操作都是一样的*****************************************************************
# 配置同步(192.168.174.5/192.168.174.2)
=================================================================服务器192.168.174.5============================================================================
#相互授权 grant replication slave, file, select on *.* to 'mstest'@'192.168.174.2' identified by 'root123'; #立即生效 flush privileges;
#退出数据库vi /etc/my.cnf
basedir=/usr/local/mysql datadir=/usr/local/mysql/data #可以为任意自然数,必须保证两台mysql主机不重复 server-id=2 log-bin=mysql-bin binlog_format=mixed binlog-do-db=auto1 binlog-ignore-db=mysql #要同步的数据库,如果需要就填,指定数据库的名称即可,默认为所有库,声明了不同步就默认除了不同步数据库意外的所有库。 replicate-do-db=auto1 replicate-ignore-db=mysql log-slave-updates slave-skip-errors=all #auto_increment_increment=2 #auto_increment_offset=2
#重启数据库
service mysql restart
#登录数据库
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | auto1 | mysql | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) #master_log_file='mysql-bin.000002',#配置是的192.168.174.2那边查到的数据 mysql> change master to master_host='192.168.174.2',master_user='mstest',master_password='root123',master_log_file='mysql-bin.000001',master_log_pos=154; Query OK, 0 rows affected, 2 warnings (0.10 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec)
====================================================================服务器192.168.174.2======================================================================
#相互授权 grant replication slave, file, select on *.* to 'mstest'@'192.168.174.5' identified by 'root123'; #立即生效 flush privileges;
#退出数据库vi /etc/my.cnf
basedir=/usr/local/mysql datadir=/usr/local/mysql/data #可以为任意自然数,必须保证两台mysql主机不重复 server-id=3 log-bin=mysql-bin binlog_format=mixed binlog-do-db=auto1 binlog-ignore-db=mysql #要同步的数据库,如果需要就填,指定数据库的名称即可,默认为所有库,声明了不同步就默认除了不同步数据库意外的所有库。 replicate-do-db=auto1 replicate-ignore-db=mysql log-slave-updates slave-skip-errors=all #auto_increment_increment=2 #auto_increment_offset=2
#重启数据库
service mysql restart
#登录数据库
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | auto1 | mysql | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) # master_log_file='mysql-bin.000002',#配置是的192.168.174.5那边查到的数据 mysql> change master to master_host='192.168.174.5',master_user='mstest',master_password='root123',master_log_file='mysql-bin.000001',master_log_pos=154; Query OK, 0 rows affected, 2 warnings (0.04 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec)
192.168.174.5下
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.174.2 Master_User: mstest Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 548 Relay_Log_File: t-enter-relay-bin.000002 Relay_Log_Pos: 517 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: auto1 Replicate_Ignore_DB: mysql 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: 548 Relay_Log_Space: 726 Until_Condition: None 。。。。。。。。。。
192.168.174.2下
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.174.5 Master_User: mstest Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 548 Relay_Log_File: py-my-relay-bin.000002 Relay_Log_Pos: 517 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: auto1 Replicate_Ignore_DB: mysql 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: 548 Relay_Log_Space: 724 Until_Condition: None 。。。。。。。。
#这两个指标为yes说明配置完成
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证一下
在192.168.174.5上的auto1库下创建table1,在192.168.174.2上查询auto1库下的库,有table1 在192.168.174.2上的auto1库下创建table2,在192.168.174.5上查询auto1库下的库,有table2
标签:同步,Log,主主,mysql5.7,192.168,master,usr,mysql,local 来源: https://www.cnblogs.com/quanyao/p/13729482.html