mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
| test |
+--------------------+
查看slave主机中从库有哪些数据库
[root@localhost ~]# mysql -uroot -p'123456' -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
确保从数据库与主数据库里的数据一样,先全备主数据库并还原到从数据库中
## 全备主库时,给数据库加上读锁,避免在备份期间有其他人写入数据,导致数据不一致 ##
[root@localhost ~]# mysql -uroot -p'123456'
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.03 sec)
注:锁表后另开终端备份主库,退出数据库后将解除锁表状态
## 备份主库,并将备份文件传送到从库 ##
[root@localhost ~]# mysqldump -uroot -p'123456' --all-databases > /opt/all-$(date +'%Y%m%d-%H%M%S').sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ll /opt
total 788
-rw-r--r--. 1 root root 801219 Nov 10 00:00 all-20181110-000010.sql
[root@localhost ~]# scp /opt/all-20181110-000010.sql root@192.168.91.130:/root
root@192.168.91.130's password:
all-20181110-000010.sql
在从库上恢复主库的备份并查看从库有哪些库,确保与主库一致
[root@localhost ~]# mysql -uroot -p'123456' < all-20181110-000010.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -p'123456' -e "show databases"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| student |
| sys |
| test |
+--------------------+
主从配置
在主(master)数据库中创建一个同步账号授权给数据库使用
mysql> create user 'repl'@'192.168.91.130' identified by 'repl123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'repl'@'192.168.91.130';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -uroot -p'123456' -e "select * from student.haha"
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+------+
| id | name |
+------+------+
| 1 | n |
| 2 | x |
+------+------+