MySQL主从
Posted 村里唯一的运维工程师
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL主从相关的知识,希望对你有一定的参考价值。
1、准备两台服务器 centos7
192.168.52.35
192.168.52.36
2、关闭防火墙
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# setenforce 0
3、两台都下载mysql
yum -y install mariadb mariadb-server
4、编辑MySQL配置文件
第一台:
[root@localhost ~]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd server-id=1 #添加 log-bin=mysql-bin #添加 relay-log=mysql-relay #添加
第二台:
[root@localhost ~]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd server-id=2 #添加 log-bin=mysql-bin #添加 relay-log=mysql-relay #添加
5、开启mysql服务
systemctl restart mariadb
6、进入第一台主的服务器
[root@localhost ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \\g. Your MariaDB connection id is 2 Server version: 5.5.64-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. MariaDB [(none)]> create user tom; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> grant all on *.* to\'tom\'@\'192.168.52.36\' identified by \'123\'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 529 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) MariaDB [(none)]>
打开第二台的mysql
[root@localhost ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \\g. Your MariaDB connection id is 2 Server version: 5.5.64-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type \'help;\' or \'\\h\' for help. Type \'\\c\' to clear the current input statement. MariaDB [(none)]> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) MariaDB [(none)]> change master to -> master_host=\'192.168.52.35\', -> master_password=\'123\', -> master_user=\'tom\', -> master_log_file=\'mysql-bin.000003\', -> master_log_pos=529; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec)
Slave_IO_Running: Yes #这俩个要变成yes才算成功
Slave_SQL_Running: Yes
以上是关于MySQL主从的主要内容,如果未能解决你的问题,请参考以下文章