Mysql主從配置,一主一從

主庫IP:192.168.1.156 Hostname:CNDB01
從庫IP:192.168.1.157 Hostname:CNDB02
系統:Centos7.4
Mysql:Mariadb 5.7mysql

1.安裝:
yum -y install mysqlsql

2.配置主服務器mysql文件:
#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[mysqld]
server-id = 1 #主爲1
log-bin = mysql-bin #開啓二進制日誌
skip-gran-tables #開啓無密碼登陸,不啓用會有可能鏈接不上
!includedir /etc/my.cnf.d數據庫

3.配置數據庫參數,建立sync同步帳戶,而且受權slave權限
#systemctl start mysql
#mysql -u root -p
MySQL [(none)]> grant replication slave on . to 'sync'@'192.168.1.156' identified by 'Aa123456!';
MySQL [(none)]>exit
設置完重啓數據庫
#systemctl stop mysql
#systemctl start mysqlvim

4.再次鏈接數據庫查看設置好的主服務請的參數,記住表中的2個參數,須要在從服務器中用到
#mysql -u root -p
MySQL [(none)]> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)服務器

5.從庫配置安裝:
yum -y install mysqlsocket

6.配置主服務器mysql文件:
#systemctl start mysql
#vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
[mysqld]
server-id = 2 #從爲2
!includedir /etc/my.cnf.dide

7.設置完重啓數據庫
#systemctl stop mysql
#systemctl start mysql測試

8.鏈接數據庫配置數據庫
#systemctl start mysql
#mysql -u root -p
MySQL [(none)]> change master to
master_host='192.168.1.156',master_user='sync',master_password=' Aa123456!',master_log_file='mysql-bin.000004',master_log_pos=154;
MySQL [(none)]>slave start;
MySQL [(none)]>show slave status\G (\G不要加分號;)日誌

9測試:
在主服務器上新建庫和表
#mysql -uroot -p
MySQL [(none)]> create database mysql_test charset=utf8;
Query OK, 1 row affected (0.00 sec)orm

MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mysql_test |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> use mysql_test;
Database changed
MySQL [mysql_test]> create table t0 (id varchar(20),name varchar(30));
Query OK, 0 rows affected (0.00 sec)

MySQL [mysql_test]> show tables;
+----------------------+
| Tables_in_mysql_test |
+----------------------+
| t0 |
+----------------------+
1 row in set (0.00 sec)

在從庫上查詢

Mysql主從配置,一主一從

相關文章
相關標籤/搜索