linux搭建mysql集群

背景:有三台服务器master,slaver1,slaver2,想要搭建一个mysql数据库集群。

具体步骤:

1.首先要在三台服务器上安装好单机版的mysql,版本什么的都随意。

2.编辑/etc/my.cnf文件,这是mysql最重要的配置文件

在[mysqld]下面添加如下两行
log-bin=mysql-bin
server-id=18

其中需要注意的时server-id三台服务器不能相同

3.登录mysql命令行,执行如下命令

grant replication slave on *.* to root @'slaver1的ip' identified by 'root';

MariaDB [mysql]> grant replication slave on  *.* to root@'slaver1的ip' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

grant replication slave on *.* to root @'slaver2的ip' identified by 'root';

MariaDB [mysql]> grant replication slave on  *.* to root@'slaver2的ip' identified by 'root';
Query OK, 0 rows affected (0.00 sec)

再执行show master status;命令查出以下内容,供下一步使用

MariaDB [mysql]> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      459 |              |                  |
+------------------+----------+--------------+------------------+

4.在两台从服务器上分别执行如下命令,来绑定主从关系,如果执行失败可以执行flush privileges命令来刷新一下

MariaDB [mysql]> change master to master_user='root',master_password='root',master_host='master',master_log_file='mysql-bin.000001',master_log_pos=459;
Query OK, 0 rows affected (0.01 sec)

绑定之后可以通过以下命令查看刚刚的操作有没有执行成功。

[root@slave-1 ~]# cd /var/lib/mysql/
[root@slave-1 mysql]# cat master.info 
18
mysql-bin.000001
459
master
root
root
3306
60
0

5.最后还要执行show slave status\G;命令验证一下两个参数

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

如果都是yes,则成功了。

MariaDB [mysql]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: master
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 673
               Relay_Log_File: mariadb-relay-bin.002150
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: 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: 673
              Relay_Log_Space: 1448
              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: 18
1 row in set (0.00 sec)

最后感谢一下up主的支持https://www.bilibili.com/video/BV1Kt411M7vn?t=337

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