十五週二次課mysql
17.1mysql主從介紹linux
17.2準備工做web
17.3配置主sql
17.4配置從數據庫
17.5測試主從同步vim
17.1mysql主從介紹網絡
原理很簡單:從會把主上的binlog搞到從上來,從再根據這個binlog生成本身的中繼日誌,而後再根據中繼日誌執行相應的更改,最終達到兩邊的數據一致。dom
17.2準備工做socket
1.首先下載二進制免編譯的包,下載到/usr/local/src/目錄下ide
2.解壓壓縮包
3.解壓完以後,把解壓出來的目錄放到 /usr/local/mysql/ 目錄下
[root@hanfeng ~]# ls /usr/local/mysql bin data include man my-new.cnf README share support-files COPYING docs lib my.cnf mysql-test scripts sql-bench [root@hanfeng ~]#
4.而後切換到 /usr/local/mysql/ 目錄下,進行初始化 命令
5.初始化成功的標誌就是兩個OK,或者用 echo $? 檢查是否初始化成功
6.編輯 /etc/my.cnf 文件——>默認是自帶 my.cnf 文件的
在 /etc/my.cnf 文件中 定義 datadir=/data/mysql 定義 socket=/tmp/mysql.sock
7.拷貝啓動腳本
8.編輯啓動腳本
vim /etc/init.d/mysqld //對如下兩行進行指定路徑 指定basedir的路徑 /usr/local/mysql 指定datadir的路徑 /data/mysql
9.以後就能夠啓動mysql了
10.若是啓動失敗,能夠去查看錯誤日誌
11.建議 :
12.若想開機啓動,只須要輸入命令
1.在兩臺機器安裝並啓動mysql服務後,首先在主上進行操做(主爲192.168.23.136,從爲192.168.23.130)。
2.修改/etc/my.cnf文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=136
log_bin=aminglinux1
# 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
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
保存退出
3.更改完配置之後,須要重啓mysql服務。
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.......... SUCCESS!
[root@localhost ~]#
若是重啓過程當中出現「Plugin 'FEDERATED' is disabled」錯誤,刪除/data目錄下除mysql之外的目錄便可
4.查看/data/mysql目錄下,會生成一些文件
[root@localhost mysql]# ls -lt /data/mysql
total 110656
-rw-rw---- 1 mysql mysql 120 Jun 17 14:23 aminglinux1.000001
-rw-rw---- 1 mysql mysql 21 Jun 17 14:23 aminglinux1.index
-rw-rw---- 1 mysql mysql 56 Jun 5 10:09 auto.cnf
drwxr-xr-x 2 root root 6 Jun 17 14:31 blog
-rw-rw---- 1 mysql mysql 12582912 Jun 17 14:23 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 17 14:23 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 5 10:08 ib_logfile1
-rw-rw---- 1 mysql mysql 36693 Jun 17 14:23 localhost.localdomain.err
-rw-rw---- 1 mysql mysql 5 Jun 17 14:23 localhost.localdomain.pid
drwx------ 2 mysql mysql 4096 Jun 5 10:08 mysql
drwx------ 2 mysql mysql 4096 Jun 17 11:08 mysql2
drwx------ 2 mysql mysql 4096 Jun 5 10:08 performance_schema
drwx------ 2 mysql mysql 6 Jun 5 10:08 test
[root@localhost mysql]#
5.測試,準備作一個數據作演示看
6.首先作一個備份
[root@localhost mysql]# mysqldump -uroot -paminglinux blog > /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
7.建立一個新的數據庫
[root@localhost mysql]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
8.建立好數據庫之後,還須要把數據恢復一下,也就是說作的主從,參考對象是aming數據庫
[root@localhost mysql]# mysql -uroot -paminglinux aming < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
9.再次查看/data/mysql目錄下的文件
[root@localhost mysql]# ls -l /data/mysql
total 110656
drwx------ 2 mysql mysql 20 Jun 17 14:33 aming
-rw-rw---- 1 mysql mysql 217 Jun 17 14:33 aminglinux1.000001
-rw-rw---- 1 mysql mysql 21 Jun 17 14:23 aminglinux1.index
-rw-rw---- 1 mysql mysql 56 Jun 5 10:09 auto.cnf
drwxr-xr-x 2 root root 6 Jun 17 14:31 blog
-rw-rw---- 1 mysql mysql 12582912 Jun 17 14:23 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 17 14:23 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 5 10:08 ib_logfile1
-rw-rw---- 1 mysql mysql 36693 Jun 17 14:23 localhost.localdomain.err
-rw-rw---- 1 mysql mysql 5 Jun 17 14:23 localhost.localdomain.pid
drwx------ 2 mysql mysql 4096 Jun 5 10:08 mysql
drwx------ 2 mysql mysql 4096 Jun 17 11:08 mysql2
drwx------ 2 mysql mysql 4096 Jun 5 10:08 performance_schema
drwx------ 2 mysql mysql 6 Jun 5 10:08 test
[root@localhost mysql]#
10.正常狀況下,能看到aminglinux1.000001二進制文件是由增長的,增長的大小是和blog庫保持一致的,aminglinux1.000001完整地記錄了數據庫的過程。建立的庫,建立的表,以及表裏的內容全都有。
11.下面建立主從相互同步用的用戶
12.進入mysql裏面去
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
13.建立用戶
mysql> grant replication slave on *.* to 'repl'@'192.168.23.130' identified by 'aminglinux111';
Query OK, 0 rows affected (0.00 sec)
mysql>
14.鎖定表,目的是不讓表繼續寫了,數據到此暫停,先把這個狀態保持在這兒。由於一下子要作從的機器配置,須要兩臺機器同步,保證兩臺機器的數據一致,同步纔不會出錯
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.10 sec)
mysql>
15.查看一下binlog文件的大小,記住名字和位置
mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 | 428 | | | |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
16.而後退出數據庫,作一個數據同步
17.查看/data/mysql下都有哪些庫,主上有哪些庫,從上就要同步哪些庫,意味着數據都有備份過去
[root@localhost mysql]# ls
aming aminglinux1.index blog ib_logfile0 localhost.localdomain.err mysql performance_schema
aminglinux1.000001 auto.cnf ibdata1 ib_logfile1 localhost.localdomain.pid mysql2 test blog
[root@localhost mysql]#
18.備份數據庫,除了mysql庫,由於mysql庫裏面有帳號密碼,從上的時候不可能把全部權限複製過去,因此mysql不須要備份
[root@localhost mysql]# mysqldump -uroot -paminglinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
19.一下子把/tmp/下.sql文件都拷貝到從上去
[root@localhost mysql]# ls /tmp/*.sql
/tmp/blog.sql /tmp/my2.sql /tmp/mysql2.sql /tmp/mysql_all.sql /tmp/mysqlbak.sql /tmp/user.sql /tmp/zrlog.sql
20.主上操做完成,接下來從上操做
1.首先在從上安裝並啓動mysql,而後查看my.cnf,配置server-id=130,要求和主不同,在配置文件的log_bin參數就不須要配置的,由於只有主上才須要二進制日誌文件
[root@tianqi-01 ~]# vim /etc/my.cnf
[mysqld]
character_set_server = utf8
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=130
log_bin=aminglinux1
# 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
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
2.重啓mysql服務
[root@tianqi-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL....... [ 肯定 ]
Starting MySQL...................... [ 肯定 ]
[root@tianqi-01 ~]#
3.增長server-id後,對mysql是沒有變化的
[root@tianqi-01 ~]# ls /data/mysql
aming aminglinux1.000007 aminglinux1.000014 aminglinux1.000021 aminglinux1.000028 ib_logfile0 tianqi-01.pid
aminglinux1.000001 aminglinux1.000008 aminglinux1.000015 aminglinux1.000022 aminglinux1.000029 ib_logfile1 zabbix
aminglinux1.000002 aminglinux1.000009 aminglinux1.000016 aminglinux1.000023 aminglinux1.000030 mysql zrlog
aminglinux1.000003 aminglinux1.000010 aminglinux1.000017 aminglinux1.000024 aminglinux1.index mysql2
aminglinux1.000004 aminglinux1.000011 aminglinux1.000018 aminglinux1.000025 auto.cnf performance_schema
aminglinux1.000005 aminglinux1.000012 aminglinux1.000019 aminglinux1.000026 db1 test
aminglinux1.000006 aminglinux1.000013 aminglinux1.000020 aminglinux1.000027 ibdata1 tianqi-01.err
[root@tianqi-01 ~]#
4.把主機器上備份的 .sql 數據,拷貝到從機器上,而後作一個數據恢復
[root@tianqi-01 ~]# scp 192.168.23.136:/tmp/*.sql /tmp/
The authenticity of host '192.168.23.136 (192.168.23.136)' can't be established.
ECDSA key fingerprint is SHA256:A62fDLxjGpEeD/g4UPe/2LtbDTOkiS9zpWjO7w9tZGc.
ECDSA key fingerprint is MD5:04:16:78:f6:50:ad:0a:cf:60:57:b2:b0:cb:2e:f8:4c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.23.136' (ECDSA) to the list of known hosts.
root@192.168.23.136's password:
blog.sql 100% 1258 1.1MB/s 00:00
my2.sql 100% 638KB 3.7MB/s 00:00
mysql2.sql 100% 30KB 1.8MB/s 00:00
mysql_all.sql 100% 1276KB 2.7MB/s 00:00
mysqlbak.sql 100% 638KB 9.0MB/s 00:00
user.sql 100% 7026 5.8MB/s 00:00
zrlog.sql 100% 1259 950.7KB/s 00:00
[root@tianqi-01 ~]#
5.建立數據庫
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 blog;
Query OK, 1 row affected (0.00 sec)
mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql>
6.而後將數據作一個恢復
[root@tianqi-01 ~]# mysql -uroot -paminglinux blog < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux zrlog < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux mysql2 < /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux aming < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]#
7.而後查看/data/mysql/目錄下的數據是否和主機器上的/data/mysql/目錄是否一致
8.開始實現主從
9.在從機器登陸到mysql
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
10.而後在數據庫裏面執行命令,中止slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
11.進行主機器相關配置
mysql> change master to master_host='192.168.23.136', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000001', master_log_pos=428;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql>
12.開始slave
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql>
13.這時候經過 show slave status\G 判斷主從是否配置成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.23.136
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: aminglinux1.000001
Read_Master_Log_Pos: 428
Relay_Log_File: tianqi-01-relay-bin.000002
Relay_Log_Pos: 285
Relay_Master_Log_File: aminglinux1.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: 428
Relay_Log_Space: 462
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: 136
Master_UUID: 8c4ee77b-6865-11e8-8647-000c2970f861
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.01 sec)
mysql>
注意:這裏可能會出現Slave_IO_Running: Connecting錯誤,主要緣由有三個:一、網絡不通二、密碼不對三、pos不對,仔細檢查發現這3項沒有錯誤以後,檢查一下主的防火牆是或關閉
14.解鎖主上的表
mysql> unlock tables;
Query OK, 0 rows affected (0.09 sec)
15.到這裏主從搭建就算完成了
1.主進入MySQL,使用aming數據庫
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use aming;
Database changed
2.建立數據表t1
mysql> create table t1(`id` int(4),`name` char(20));
Query OK, 0 rows affected (0.66 sec)
3.查看數據表t1
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.10 sec)
mysql>
4.從上 進入MySQL,使用aming數據庫
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2092
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| mysql |
| mysql2 |
| performance_schema |
| test |
| zrlog |
+--------------------+
8 rows in set (0.05 sec)
mysql> use aming;
Database changed
5.查看全部數據表
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
6.查看數據表t1的建表語句,結果與主的一致,說明主從一直
mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.20 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>