在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
搭建两台MySQL服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库 | 192.168.66.128 | centos7/redhat7 mysql-5.7 |
有数据 |
从数据库 | 192.168.66.129 | centos7/redhat7 mysql-5.7 |
无数据 |
有数据是指如果到了一个新的公司会有数据
所以利用从先备份
分别在主从两台服务器上安装mysql-5.7版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章
为确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
//先查看主库有哪些库
[root@master ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zjy |
+--------------------+
//再查看从库有哪些库
[root@slave ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
//全备主库
//全备主库时需要另开一个终端,给数据库加上读锁,避免在备份期间有其他人在写入导致数据不一致
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.18 sec)
mysql>
//此锁表的终端必须在备份完成以后才能退出
//备份主库并将备份文件传送到从库
[root@master ~]# mysqldump -uroot -p1 --all-databases > 1.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@master ~]# ls
1.sql mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
anaconda-ks.cfg
[root@master ~]# scp 1.sql root@192.168.66.129:/root/
root@192.168.66.129's password:
1.sql 100% 782KB 10.9MB/s 00:00
//解除主库的锁表状态,直接退出交互式界面即可
mysql> quit
Bye
//在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@slave ~]# mysql -uroot -p1 < 1.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@slave ~]# mysql -uroot -p1 -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| zjy |
+--------------------+
mysql> create user 'zjy'@'192.168.66.129' identified by '1';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'zjy'@'192.168.66.129';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@master ~]# vim /etc/my.cnf
log-bin=mysql_bin //启用binlog日志
server-id=1 //数据库服务器唯一标识符,主库的server-id值必须比从库的小
//重启mysql服务
[root@master ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
//查看主库的状态
mysql> show master status\G
*************************** 1. row ***************************
File: mysql_bin.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
[root@slave ~]# vim /etc/my.cnf
//添加如下内容
server-id=2 //设置从库的唯一标识符,从库的server-id值必须大主库的该值
relay-log=relay_log //启用中继日志relay-log
//重启从库的mysql服务
[root@slave ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL..... SUCCESS!
//配置并启动主从复制
mysql> change master to
-> master_host='192.168.99.128',
-> master_user='zjy',
-> master_password='1',
-> 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)
//查看从服务器状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.99.128
Master_User: zjy
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
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: 740
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: 1
Master_UUID: 682ffa6d-b951-11e9-b5cc-000c29cfb901
Master_Info_File: /opt/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:
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)
在主服务器的student库的bj2表中插入数据:
ysql> select * from student;
Empty set (0.00 sec)
mysql> insert into student values (1,'sean',20),(2,'tom',23),(3,'jerry',30);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from student;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)
在从数据库中查看数据是否同步:
mysql> use zjy;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from studnet;
+----+-------+------+
| id | name | age |
+----+-------+------+
| 1 | sean | 20 |
| 2 | tom | 23 |
| 3 | jerry | 30 |
+----+-------+------+
3 rows in set (0.00 sec)