云原生--数据库第二次作业

完成MySQL主从复制。

主库设置
1.设置server-id值并开启binlog参数

[root@server ~]# vim /etc/my.cnf.d/mysql-server.cnf 
[mysqld]
log_bin = mysql-bin
server_id = 120

重启数据库

2.建立同步账号
(1)创建用户并给予权限

mysql> create user hjj identified with mysql_native_password by 'sy@hjj123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'hjj'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

(2)查看mysql主服务器信息

mysql> show master status;
+---------------+----------+--------------+------------------+-------------------------------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                         |
+---------------+----------+--------------+------------------+-------------------------------------------+
| binlog.000039 |      864 |              |                  | a58df9d1-11ff-11ed-803f-000c296ee409:1-55 |
+---------------+----------+--------------+------------------+-------------------------------------------+
1 row in set (0.00 sec)

从服务器配置
1.设置server-id必须和主服务器不同

[root@localhost ~]# vim /etc/my.cnf
[mysqld]
log_bin = mysql-bin
server_id = 2222

2.创建与主服务器相同的用户并给予权限

mysql> create user hjj identified with mysql_native_password by 'sy@hjj123';
Query OK, 0 rows affected (0.06 sec)

mysql> grant replication slave on *.* to 'hjj'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

(3)配置主服务器的信息,master_log_pos为查询到的主服务器的position,master_log_file为查询到的日志命

mysql> change master to master_host='192.168.40.128',
    -> master_port=3306,
    -> master_user='hjj',
    -> master_password='sy@hjj123',
    -> master_log_pos=864,
    -> master_log_file='binlog.000039';
Query OK, 0 rows affected, 9 warnings (0.02 sec)

(4)启动从服务器(停掉主服务器的防火墙)

mysql> start slave;
Query OK, 0 rows affected, 2 warnings (0.00 sec)

(5)
查看状态

mysql> show slave status\G

  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes

你可能感兴趣的:(数据库,服务器,mysql)