replication slave:指定授予的权限类型为复制从服务器的权限
[root@mysql-zhu ~]# service mysql8 start //启动主的mysql服务
Starting MySQL.... SUCCESS!
[root@mysql-zhu ~]# mysql -uroot -p'123' //登录到主的mysql中
mysql> create user 'slave'@'%' identified by '123'; //创建一个slave用户,%表示在每个主机上都可以使用slave登录到该主机上的mysql
Query OK, 0 rows affected (0.06 sec)
mysql> grant replication slave on *.* to 'slave'@'%'; //将所有的数据库的所有表的replication slave操作权限给到slave用户
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges; //重新加载权限表
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock; //刷新所有表并施加一个全局读锁
Query OK, 0 rows affected (0.00 sec)
mysql> create database if not exists abc charset utf8;
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
mysql> show master status; //显示主服务器当前二进制日志的状态信息,
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000003 | 1067 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)
File:当前正在写入的二进制日志文件的名称。
Position:当前写入的位置(偏移量)在二进制日志文件中的位置。
[root@mysql-slave ~]# mysql -uslave -p123 -h10.0.0.51 -P3306 --get-server-public-key //用于以指定的用户 slave 和密码 123 连接到主机地址为 10.0.0.51 、端口为 3306 的 MySQL 服务器,并获取服务器的公共密钥
[root@mysql-slave ~]# mysql -P3310 -p123 //登录到从的数据库中
mysql> change master to
-> master_host='10.0.0.51', //指定主服务器的主机地址
-> master_user='slave', //指定用于连接主服务器进行复制的用户名
-> master_password='123', //指定对应的密码
-> master_port=3306, //指定主服务器的端口
-> master_log_file='binlog.000003', //指定从哪个二进制日志文件开始复制
-> master_log_pos=1067; //指定在指定的二进制日志文件中的起始位置
Query OK, 0 rows affected, 9 warnings (0.01 sec)
mysql> start slave; //启动从服务器的复制进程
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> show slave status\G //查看从服务器的复制状态详细信息
11、同步测试
主服务器
mysql> unlock tables; //释放之前通过 LOCK TABLES 命令施加的表锁
Query OK, 0 rows affected (0.00 sec)
mysql> create database if not exists test charset utf8mb4; //创建数据库test
Query OK, 1 row affected (0.02 sec)
mysql> use test; //使用test数据库
Database changed
mysql> create table user(id int primary key,username varchar(45) not null,passwordvarchar(45) not null); //创建user表
Query OK, 0 rows affected (0.03 sec)
mysql> insert into user values(1,'zhangsan','abc'); //向user表中插入数据
Query OK, 1 row affected (0.03 sec)
从服务器
mysql> show databases; //查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.02 sec)
mysql> use test; //使用test数据库
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> show tables; //查看所有表
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
mysql> select * from user; //查看user表的内容
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | zhangsan | abc |
+----+----------+----------+
1 row in set (0.00 sec)