MySQL主从复制 一 原理
*基于语句的复制。在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。
*基于行的复制。把改变的内容复制过去,而不是把命令在从服务器上执行一遍。
二 环境
1,准备两台CentOS 7 都安装上MySQL数据库
master 192.168.226.135
slave 192.168.226.138
2,保证两台服务器的时间一致
安装时间同步服务器(ntp服务)
三步骤 1mssql复制,在主服务器上安装时间同步服务器(ntp服务),使用从服务器去和主服务器的时间进行同步。 #主服务器配置为时间同步服务器
yum -y install ntp #使用yum源安装ntp服务vim /etc/ntp. conf #修改ntp服务配置文件
将第二行未注释的“restrict default nomodify notrap noquery”修改成:restrict default nomodify #允许任何IP 的客户机都可以进行时间同步systemctl restart ntpd #重启生效systemctl enable ntpd #设置开机自启动#从服务器配置为时间同步客户端yum -y install ntpdate #安装ntpdatentpdate 192. 168.226.135 #和主服务器的时间进行同步 2 安装myaql数据库5.7
#配置MySQL5.7源
wget 下载
rmp -ivh mysql80-community-release-el7-7.noarch.rpm 安装
#安装MySQL
yum -y install mysql-community- server
systemctl restart mysqld #重启一下数据库保证能够登录到MySQL
grep “password” /var/log/mysqld.log #查询初始随机密码
mysql -u root -p #使用初始密码登录
password:
#修改源文件
vim /etc/yum.repos.d/mysql-community.repo #修改配置文件
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1 #启用5.7版本MySQL
gpgcheck=0 #不检查错误
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0 #不启用8.0版本MySQL
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022
file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
yum makecache #重新生成缓存mysql> set global validate_ password_ policy=0; #密码策略全局变量改掉mysql> alter user user() identified by ‘xxx’; #设置新密码,默认情况下user表中的user字段只有root一个用户mysql>select version(); #查看数据库的版本mysql>grant all on . to ‘root’@‘%’ identified by ‘xxx’; #创建允许远程登陆mysql>flush privileges; #刷新策略 3 主服务器配置 vim /etc/my.cnf #修改配置文件 在文末增加下面内容
server-id = 11
log_ _bin = master- bin
log-slave-updates = true
mysq|> grant replication slave on . to ‘myslave @’ 192.168.226. %’ identified by ‘xxx’; #为从服务器添加授权mysql> flush privileges; #刷新用户授权
Query OK, 0 rows affected (0.01 sec)mysq|> show master status; #查看master状态,找到position字段信息,这里是154
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000003 | 154 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#File 主服务器的二进制日志文件名
#Position 主服务器的修改记录
4、配置slave从服务器 vim /etc/my .cnf #在文末增加下面内容
server-id = 22
relay-log = relay-log-bin
relay-log-index = slave-relay-bin. index
systemctl restart mysqld #登录从服务器的MySQL上,配置同步
mysq|> change master to master_ host=’ 192.168.226.135’ ,master_ user=’ myslave ,master_ password= ‘xxx’ ,master_ log_ file=‘master-bin. 000003’ ,master_ log_ pos= 154;
#这里的master_ log_ pos=154 为master服务器上position字段信息mysq|> start slave; #开启服务mysq|> show slave status\G; 查看是否成功
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.226.135
Master_User: myslave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 154
Relay_Log_File: relay-log-bin.000002
Relay_Log_Pos: 324
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes **#为yes成功**
Slave_SQL_Running: Yes **#为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: 154
Relay_Log_Space: 532
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: 11
Master_UUID: c25c6b7b-3fc4-11ed-8f87-000c296435cb
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica 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:
Master_public_key_path:
Get_master_public_key: 0
Network_Namespace:
1 row in set, 1 warning (0.01 sec)
ERROR:
No query specified
在master上创建数据库,然后在slave上就可以查看到对应的数据库
在master上创建wangwu数据库
mysql> create database wangwu;
Query OK, 1 row affected (0.05 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| 1 |
| mysql |
| performance_schema |
| student |
| sys |
| wangwu |
| yy |
+--------------------+
8 rows in set (0.00 sec)
在slave上查看就有wangwu数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wangwu |
+--------------------+
5 rows in set (0.03 sec)
(编辑:通辽站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|