2018-05-16 Linux學習
17.1 MySQL主從介紹
MySQL主從又叫做Replication、AB復制。簡單講就是A和B兩臺機器做主從後,在A上寫數據,另外一臺B也會跟著寫數據,兩者數據實時同步的 MySQL主從是基於binlog的,主上須開啟binlog才能進行主從。 主從過程大致有3個步驟 1)主將更改操作記錄到binlog裏 2)從將主的binlog事件(sql語句)同步到從本機上並記錄在relaylog裏 3)從根據relaylog裏面的sql語句按順序執行 主上有一個log dump線程,用來和從的I/O線程傳遞binlog 從上有兩個線程,其中I/O線程用來同步主的binlog並生成relaylog,另外一個SQL線程用來把relaylog裏面的sql語句落地
17.2 準備工作
操作過程
cd /usr/local/src wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz tar zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql cd /usr/local/mysql useradd -s /sbin/nologin mysql (或 useradd -r -m -d /data/mysql -s /sbin/nologin mysql ) mkdir /data/mysql ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql cp support-files/my-default.cnf /etc/my.cnf cp support-files/mysql.server /etc/init.d/mysqld vim /etc/my.cnf 定義 datadir&socket [mysqld] datadir=/data/mysql socket=/tmp/mysql.sock vi /etc/init.d/mysqld 定義 basedir 和 datadir basedir=/usr/local/mysql datadir=/data/mysql chmod 755 /etc/init.d/mysqld /etc/init.d/mysqld start
17.3 配置主
主從配置 - 主上操作
安裝mysql 修改my.cnf,增加server-id=130和log_bin=aminglinux1 修改完配置文件後,啟動或者重啟mysqld服務 把mysql庫備份並恢復成aming庫,作為測試數據 mysqldump -uroot mysql > /tmp/mysql.sql mysql -uroot -e “create database aming” mysql -uroot aming < /tmp/mysql.sql 創建用作同步數據的用戶 grant replication slave on *.* to ‘repl‘@slave_ip identified by ‘password‘; flush tables with read lock; show master status;
操作過程
[root@linux-01 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=160
log_bin=aminglinux01
[root@linux-01 ~]# /etc/init.d/mysqld restart
[root@linux-01 mysql]# cd /data/mysql/
[root@linux-01 mysql]# ls -lt
總用量 110680
-rw-rw----. 1 mysql mysql 50331648 4月 12 05:01 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 4月 12 05:01 ibdata1
-rw-rw----. 1 mysql mysql 55008 4月 12 05:01 linux-01.err
-rw-rw----. 1 mysql mysql 6 4月 12 05:01 linux-01.pid
-rw-rw----. 1 mysql mysql 22 4月 12 05:01 aminglinux01.index
-rw-rw----. 1 mysql mysql 120 4月 12 05:01 aminglinux01.000001
drwx------. 2 mysql mysql 4096 4月 11 05:39 zrlog
drwx------. 2 mysql mysql 4096 3月 31 21:32 mysql2
-rw-rw----. 1 mysql mysql 56 3月 26 06:01 auto.cnf
drwx------. 2 mysql mysql 4096 3月 26 05:56 mysql
drwx------. 2 mysql mysql 4096 3月 26 05:56 performance_schema
-rw-rw----. 1 mysql mysql 50331648 3月 26 05:55 ib_logfile1
drwx------. 2 mysql mysql 6 3月 26 05:55 test
[root@linux-01 mysql]# mysqldump -uroot -paminglinux mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 mysql]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 mysql]# mysql -uroot -paminglinux aming < /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 mysql]# mysql -uroot -paminglinux
mysql> grant replication slave on *.* to ‘repl‘@‘192.168.106.165‘ identified by ‘aminglinux111‘;
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| aminglinux01.000001 | 669865 | | | |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
[root@linux-01 mysql]# ls -lht
總用量 222M
-rw-rw----. 1 mysql mysql 655K 4月 12 06:21 aminglinux01.000001
-rw-rw----. 1 mysql mysql 48M 4月 12 06:19 ib_logfile0
-rw-rw----. 1 mysql mysql 76M 4月 12 06:19 ibdata1
drwx------. 2 mysql mysql 4.0K 4月 12 06:19 aming
drwxr-x---. 2 root root 19 4月 12 05:09 logs
-rw-rw----. 1 mysql mysql 54K 4月 12 05:01 linux-01.err
-rw-rw----. 1 mysql mysql 6 4月 12 05:01 linux-01.pid
-rw-rw----. 1 mysql mysql 22 4月 12 05:01 aminglinux01.index
drwx------. 2 mysql mysql 4.0K 4月 11 05:39 zrlog
drwx------. 2 mysql mysql 4.0K 3月 31 21:32 mysql2
-rw-rw----. 1 mysql mysql 56 3月 26 06:01 auto.cnf
drwx------. 2 mysql mysql 4.0K 3月 26 05:56 mysql
drwx------. 2 mysql mysql 4.0K 3月 26 05:56 performance_schema
-rw-rw----. 1 mysql mysql 48M 3月 26 05:55 ib_logfile1
drwx------. 2 mysql mysql 6 3月 26 05:55 test
[root@linux-01 mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 mysql]# mysqldump -uroot -paminglinux mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@linux-01 mysql]# mysqldump -uroot -paminglinux test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.
17.4 配置從
主從配置 - 從上操作
安裝mysql
查看my.cnf,配置server-id=132,要求和主不一樣
修改完配置文件後,啟動或者重啟mysqld服務
把主上aming庫同步到從上
可以先創建aming庫,然後把主上的/tmp/mysql.sql拷貝到從上,然後導入aming庫
mysql -uroot
stop slave;
change master to master_host=‘‘, master_user=‘repl‘, master_password=‘‘, master_log_file=‘‘, master_log_pos=xx,
start slave;
還要到主上執行 unlock tables
查看主從同步是否正常
從上執行mysql -uroot
show slave stauts\G
看是否有
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
還需關註
Seconds_Behind_Master: 0 //為主從延遲的時間
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
操作過程
從
[root@linux-02 ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=165
[root@linux-02 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@linux-02 ~]# scp 192.168.106.160:/tmp/*.sql /tmp/
[email protected]‘s password:
mysql2.sql 100% 645KB 31.7MB/s 00:00
zrlog.sql 100% 11KB 5.5MB/s 00:00
[root@linux-02 ~]# alias ‘mysql=/usr/local/mysql/bin/mysql‘
[root@linux-02 ~]# alias ‘mysqldump=/usr/local/mysql/bin/mysqldump‘
[root@linux-02 ~]# mysql -uroot
mysql> create database aming;
Query OK, 1 row affected (0.00 sec)
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql> quit
[root@linux-02 ~]# mysql -uroot mysql2 < /tmp/mysql2.sql
[root@linux-02 ~]# mysql -uroot zrlog < /tmp/zrlog.sql
[root@linux-02 ~]# mysql -uroot
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to master_host=‘192.168.106.160‘,master_user=‘repl‘,master_password=‘aminglinux111‘,master_log_file=‘aminglinux01.000001‘,master_log_pos=669865;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
主
[root@linux-01 ~]# mysql -uroot -paminglinux
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
17.5 測試主從同步
幾個配置參數
主服務器上
binlog-do-db= //僅同步指定的庫
binlog-ignore-db= //忽略指定庫
從服務器上
replicate_do_db=
replicate_ignore_db=
replicate_do_table=
replicate_ignore_table=
以上四項不建議使用
replicate_wild_do_table= //如aming.%, 支持通配符%
replicate_wild_ignore_table=
測試主從
主上 mysql -uroot aming
select count(*) from db;
truncate table db;
到從上 mysql -uroot aming
select count(*) from db;
主上繼續drop table db;
從上查看db表
操作過程
mysql> use aming;
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_aming |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
2018-05-16 Linux學習