搭建Mysql的主從環境,一台主機,一台從機
主從原理:
1、在主庫上把數據更改記錄到二進制日志(Binary Log)中,這些記錄稱為二進制日志事件。
2、從庫通過IO線程將主庫上的日志復制到自己的中繼日志(Relay Log)中。
3、從庫通過SQL線程讀取中繼日志中的事件,將其重放到自己數據上。
Mysql的一些關鍵文件的位置:
1、配置文件位置:
配置文件一般在/etc/mysql中,如下所示:
2、Binary Log所在的位置
Binary Log文件一般放在/var/lib/mysql 目錄,如下所示:
在/var/lib/mysql 下還會有各個數據庫的目錄,每個目錄下就是這個數據庫里面的表文件,例如jgyw數據庫就會對應一個jgyw目錄,目錄內容如下所示:
在/var/lib/mysql下還有一個mysql目錄,這個是自帶的mysql數據庫,里面包含很多表,內容如下:
3、log所在的位置
log一般在/var/log/mysql 目錄下,如下:
現在這個目錄下只有一個error.log,一般還會有慢查詢日志mysql-slow.log, Binary Log日志 mysql-bin.log,通用日志mysql.log。
4、字符集文件
字符集文件一般在 /usr/share/mysql 目錄,如下:
5、pid文件和sock文件
pid文件和sock文件一般在/var/run/mysqld 目錄下,如下所示:
配置主庫:
1、修改master機器的配置, 修改配置文件/etc/mysql/mysql.conf.d/mysqld.cnf
1 # 2 # The MySQL database server configuration file. 3 # 4 # You can copy this to one of: 5 # - "/etc/mysql/my.cnf" to set global options, 6 # - "~/.my.cnf" to set user-specific options. 7 # 8 # One can use all long options that the program supports. 9 # Run program with --help to get a list of available options and with 10 # --print-defaults to see which it would actually understand and use. 11 # 12 # For explanations see 13 # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 14 15 # This will be passed to all mysql clients 16 # It has been reported that passwords should be enclosed with ticks/quotes 17 # escpecially if they contain "#" chars... 18 # Remember to edit /etc/mysql/debian.cnf when changing the socket location. 19 20 # Here is entries for some specific programs 21 # The following values assume you have at least 32M ram 22 23 [mysqld_safe] 24 socket = /var/run/mysqld/mysqld.sock 25 nice = 0 26 27 [mysqld] 28 server-id=1 29 log-bin=mysql-bin 30 binlog_format=ROW 31 binlog_row_image=minimal 32 binlog-do-db=jgyw 33 # 34 # * Basic Settings 35 # 36 user = mysql 37 pid-file = /var/run/mysqld/mysqld.pid 38 socket = /var/run/mysqld/mysqld.sock 39 port = 3306 40 basedir = /usr 41 datadir = /var/lib/mysql 42 tmpdir = /tmp 43 lc-messages-dir = /usr/share/mysql 44 skip-external-locking 45 # 46 # Instead of skip-networking the default is now to listen only on 47 # localhost which is more compatible and is not less secure. 48 # bind-address = 127.0.0.1 49 bind-address=0.0.0.0 50 # 51 # * Fine Tuning 52 # 53 key_buffer_size = 16M 54 max_allowed_packet = 16M 55 thread_stack = 192K 56 thread_cache_size = 8 57 # This replaces the startup script and checks MyISAM tables if needed 58 # the first time they are touched 59 myisam-recover-options = BACKUP 60 #max_connections = 100 61 #table_open_cache = 64 62 #thread_concurrency = 10 63 # 64 # * Query Cache Configuration 65 # 66 query_cache_limit = 1M 67 query_cache_size = 16M 68 # 69 # * Logging and Replication 70 # 71 # Both location gets rotated by the cronjob. 72 # Be aware that this log type is a performance killer. 73 # As of 5.1 you can enable the log at runtime! 74 #general_log_file = /var/log/mysql/mysql.log 75 #general_log = 1 76 # 77 # Error log - should be very few entries. 78 # 79 log_error = /var/log/mysql/error.log 80 # 81 # Here you can see queries with especially long duration 82 #slow_query_log = 1 83 #slow_query_log_file = /var/log/mysql/mysql-slow.log 84 #long_query_time = 2 85 #log-queries-not-using-indexes 86 # 87 # The following can be used as easy to replay backup logs or for replication. 88 # note: if you are setting up a replication slave, see README.Debian about 89 # other settings you may need to change. 90 #server-id = 1 91 #log_bin = /var/log/mysql/mysql-bin.log 92 expire_logs_days = 10 93 max_binlog_size = 100M 94 #binlog_do_db = include_database_name 95 #binlog_ignore_db = include_database_name 96 # 97 # * InnoDB 98 # 99 # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. 100 # Read the manual for more InnoDB related options. There are many! 101 # 102 # * Security Features 103 # 104 # Read the manual, too, if you want chroot! 105 # chroot = /var/lib/mysql/ 106 # 107 # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". 108 # 109 # ssl-ca=/etc/mysql/cacert.pem 110 # ssl-cert=/etc/mysql/server-cert.pem 111 # ssl-key=/etc/mysql/server-key.pem
28行到32行是為了配置主從而新加的行,48行要改成49行的樣子,后面會詳細解釋。
2、service mysql restart 重啟主節點的mysql服務
3、創建從庫同步數據的賬號
1 grant replication slave on *.* to 'jgyw'@'192.168.61.135' identified by 'jgyw@123'; 2 flush privileges;
第一行的意思是允許host為192.168.61.135的客戶端以jgyw為用戶名,jgyw@123為密碼來訪問這個數據庫。
查看jgyw的授權:
1 show grants for jgyw@192.168.61.135;
4、查看主庫的執行狀態
show master status\G;
主庫ip:
主庫狀態:
配置從庫:
1、修改配置文件
1 # 2 # The MySQL database server configuration file. 3 # 4 # You can copy this to one of: 5 # - "/etc/mysql/my.cnf" to set global options, 6 # - "~/.my.cnf" to set user-specific options. 7 # 8 # One can use all long options that the program supports. 9 # Run program with --help to get a list of available options and with 10 # --print-defaults to see which it would actually understand and use. 11 # 12 # For explanations see 13 # http://dev.mysql.com/doc/mysql/en/server-system-variables.html 14 15 # This will be passed to all mysql clients 16 # It has been reported that passwords should be enclosed with ticks/quotes 17 # escpecially if they contain "#" chars... 18 # Remember to edit /etc/mysql/debian.cnf when changing the socket location. 19 20 # Here is entries for some specific programs 21 # The following values assume you have at least 32M ram 22 23 [mysqld_safe] 24 socket = /var/run/mysqld/mysqld.sock 25 nice = 0 26 27 [mysqld] 28 server-id=2 29 log-bin=mysql-bin 30 binlog_format=ROW 31 binlog_row_image=minimal 32 replicate-do-db=jgyw 33 # 34 # * Basic Settings 35 # 36 user = mysql 37 pid-file = /var/run/mysqld/mysqld.pid 38 socket = /var/run/mysqld/mysqld.sock 39 port = 3306 40 basedir = /usr 41 datadir = /var/lib/mysql 42 tmpdir = /tmp 43 lc-messages-dir = /usr/share/mysql 44 skip-external-locking 45 # 46 # Instead of skip-networking the default is now to listen only on 47 # localhost which is more compatible and is not less secure. 48 bind-address = 127.0.0.1 49 # 50 # * Fine Tuning 51 # 52 key_buffer_size = 16M 53 max_allowed_packet = 16M 54 thread_stack = 192K 55 thread_cache_size = 8 56 # This replaces the startup script and checks MyISAM tables if needed 57 # the first time they are touched 58 myisam-recover-options = BACKUP 59 #max_connections = 100 60 #table_open_cache = 64 61 #thread_concurrency = 10 62 # 63 # * Query Cache Configuration 64 # 65 query_cache_limit = 1M 66 query_cache_size = 16M 67 # 68 # * Logging and Replication 69 # 70 # Both location gets rotated by the cronjob. 71 # Be aware that this log type is a performance killer. 72 # As of 5.1 you can enable the log at runtime! 73 #general_log_file = /var/log/mysql/mysql.log 74 #general_log = 1 75 # 76 # Error log - should be very few entries. 77 # 78 log_error = /var/log/mysql/error.log 79 # 80 # Here you can see queries with especially long duration 81 #slow_query_log = 1 82 #slow_query_log_file = /var/log/mysql/mysql-slow.log 83 #long_query_time = 2 84 #log-queries-not-using-indexes 85 # 86 # The following can be used as easy to replay backup logs or for replication. 87 # note: if you are setting up a replication slave, see README.Debian about 88 # other settings you may need to change. 89 #server-id = 1 90 #log_bin = /var/log/mysql/mysql-bin.log 91 expire_logs_days = 10 92 max_binlog_size = 100M 93 #binlog_do_db = include_database_name 94 #binlog_ignore_db = include_database_name 95 # 96 # * InnoDB 97 # 98 # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. 99 # Read the manual for more InnoDB related options. There are many! 100 # 101 # * Security Features 102 # 103 # Read the manual, too, if you want chroot! 104 # chroot = /var/lib/mysql/ 105 # 106 # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". 107 # 108 # ssl-ca=/etc/mysql/cacert.pem 109 # ssl-cert=/etc/mysql/server-cert.pem 110 # ssl-key=/etc/mysql/server-key.pem
第18行到32行是新加的。
2、重啟mysql服務 service mysqld restart
3、執行同步命令
1 # 設置主服務器ip,同步賬號密碼,同步位置 2 change master to master_host='192.168.61.140',master_user='jgyw',master_password='jgyw@123',master_log_file='mysql-bin.000001',master_log_pos=145; 3 4 # 開啟同步功能 5 start slave;
其中master_host是主服務器的ip,master_user是在主服務器上為這個從機設置的用戶名,master_password是在主服務器上為這個從機設置的密碼。
4、查看從機的狀態
1 show slave status\G;
從庫ip:
從庫狀態:
可以看到Slave_IO_Running和Slave_SQL_Running都是yes。說明同步成功了。
如果執行stop slave,那么這兩行都為No,說明主從不同步。 需要執行start slave再次同步。
可能出現的問題:
1、master配置文件的第48行是默認配置,默認是監聽在127.0.0.1上,這個地址是環回地址,這樣的話master數據庫就只能接受本機的連接,而不能接受其他機器的連接,表現出來的形式就是從庫執行完start slave之后,show slave status一直顯示connecting,但是始終連接不上。這時可以在從庫所在的機器上用mysql命令嘗試去連接一下,例如 mysql -uusername -h ip -p。
如果連接不上,那么就有可能是主庫的配置文件中配置了監聽環回地址。這時只需要修改成通配地址即可。
如果修改成通配地址還是連接不上,那么可能是主庫的授權賬戶有問題,使用主庫進行授權時需要確認,授權的地址是從庫的ip,用戶名和密碼也是給從庫分配的,而這個用戶名和密碼和主庫所在的機器的用戶名和密碼沒有任何關系,這一點需要注意。
授權如果檢查沒有問題了,這是還是連接不上,那么很有可能是從庫進行change master設置時有問題。用change master進行設置時,master_host一定是主庫所在機器的ip地址,master_user是主庫為這個從庫授權的用戶名,master_password是主庫為從庫授權的用戶名對應的密碼,這一點不要弄錯了。
以上幾點設置好之后基本就可以連接上了,參考:https://blog.csdn.net/qq_34355232/article/details/88396574