mysql主从安装配置
1、mysql主從復制介紹
1、簡介
???? ????主從復制,主要是實現數據庫的數據安全、提升IO性能和讀寫分離功能,建立一個和主數據庫一致的數據庫環境,稱為從數據庫,可以使用一臺或多臺服務器充當從數據庫服務器,主服務器中的數據自動準實時復制到從服務器之中,從數據庫可以是多級復制。
2、mysql復制的類型
1.STATEMENT模式(SBR)
每一條會修改數據的sql語句會記錄到binlog中。優點是并不需要記錄每一條sql語句和每一行的數據變化,減少了binlog日志量,節約IO,提高性能。缺點是在某些情況下會導致master-slave中的數據不一致(如sleep()函數, last_insert_id(),以及user-defined functions(udf)等會出現問題)
ROW模式(RBR)
不記錄每條sql語句的上下文信息,僅需記錄哪條數據被修改了,修改成什么樣了。而且不會出現某些特定情況下的存儲過程、或function、或trigger的調用和觸發無法被正確復制的問題。缺點是會產生大量的日志,尤其是alter table的時候會讓日志暴漲。
MIXED模式(MBR)
以上兩種模式的混合使用,一般的復制使用STATEMENT模式保存binlog,對于STATEMENT模式無法復制的操作使用ROW模式保存binlog,MySQL會根據執行的SQL語句選擇日志保存方式。
3、同步原理
1.Master 數據庫只要發生變化,立馬記錄到Binary log 日志文件中
2.Slave數據庫啟動一個I/O thread連接Master數據庫,請求Master變化的二進制日志
3.Slave I/O獲取到的二進制日志,保存到自己的Relay log 日志文件中。
4.Slave 有一個 SQL thread定時檢查Realy log是否變化,變化那么就更新數據
2、mysql安裝
???? ????在配置主從之前需要將主從的mysql數據庫安裝好,安裝方式相同。
1、下載好安裝包上傳到服務器,并解壓到mysql目錄。
2、創建mysql用戶,并授權。
[root@host-10-253-234-32 ~]# useradd -r -s /sbin/nologin mysql chown -R mysql:mysql /u01/mysql5.7 [root@host-10-253-234-32 ~]# chown -R mysql:mysql /u01/mysql5.7 [root@host-10-253-234-32 mysql5.7]#3、配置my.cnf文件
vi /etc/my.cnf [root@host-10-253-234-32 mysql5.7]# cp /etc/my.cnf /etc/my.cnf.bak [root@host-10-253-234-32 mysql5.7]# vi /etc/my.cnf [root@host-10-253-234-32 mysql5.7]# more /etc/my.cnf [client] socket=/u01/mysql5.7/mysql.sock [mysqld] user=mysql sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION max_allowed_packet=20M max_connections=1000 lower_case_table_names=1 basedir=/u01/mysql5.7 datadir=/u01/mysql5.7/data socket=/u01/mysql5.7/mysql.sock character_set_server=utf8 # 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=/u01/mysql5.7/log/mysql.err pid-file=/u01/mysql5.7/mysql.pid# # include all files from the config directory # !includedir /etc/my.cnf.d [root@host-10-253-234-32 mysql5.7]#4、安裝,并記住隨機生成的密碼,首次登陸需要用到
[root@host-10-253-234-32 mysql5.7]# [root@host-10-253-234-32 mysql5.7]# bin/mysqld --initialize --user=mysql --basedir=/u01/mysql5.7 --datadir=/u01/mysql5.7/data 2020-07-18T01:35:21.513029Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-07-18T01:35:21.720049Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-07-18T01:35:21.762184Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-07-18T01:35:21.821359Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f1f4a61a-c896-11ea-8c77-fa163eae1a0e. 2020-07-18T01:35:21.822556Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-07-18T01:35:22.614278Z 0 [Warning] CA certificate ca.pem is self signed. 2020-07-18T01:35:22.924924Z 1 [Note] A temporary password is generated for root@localhost: Dc(hW5-o>JoN [root@host-10-253-234-32 mysql5.7]#5、配置ssl加密bin/mysql_ssl_rsa_setup --datadir=/u01/mysql5.7
6、配置mysql啟動服務
7、啟動服務
/etc/init.d/mysql start service start mysql #linux 6啟動命令 systemctl start mysql #linux 7啟動命令8、使用自動生成的密碼Dc(hW5-o>JoN登陸數據庫,然后修改root密碼及設置允許遠程登陸,要不然會報錯ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement
[root@host-10-253-234-32 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 47966 Server version: 5.7.31-log MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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> mysql> set password=password('password@1316'); Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> grant all privileges on *.* to root@'%' identified by 'password@1316'; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)mysql>3、配置主從同步
1)主庫配置my.cnf,添加如下參數
#Master config server_id=1 log_bin=mysql-bin binlog_format=MIXED binlog_ignore_db=information_schema,mysql,performance_schema,sys# 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=/u01/mysql5.7/log/mysql.err pid-file=/u01/mysql5.7/mysql.pid# # include all files from the config directory # !includedir /etc/my.cnf.d2)配置主庫
[[client] socket=/u01/mysql5.7/mysql.sock [mysqld] user=mysql sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION max_allowed_packet=20M max_connections=1000 lower_case_table_names=1 basedir=/u01/mysql5.7 datadir=/u01/mysql5.7/data socket=/u01/mysql5.7/mysql.sock character_set_server=utf8#Master config server_id=1 log_bin=mysql-bin binlog_format=MIXED binlog_ignore_db=information_schema,mysql,performance_schema,sys# 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=/u01/mysql5.7/log/mysql.err pid-file=/u01/mysql5.7/mysql.pid# # include all files from the config directory # !includedir /etc/my.cnf.d#設置日志超時時間 [root@host-10-253-234-32 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 46967 Server version: 5.7.31-log MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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 variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 0 | +------------------+-------+ 1 row in set (0.00 sec)mysql> mysql> set global expire_logs_days=15; Query OK, 0 rows affected (0.00 sec)mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 15 | +------------------+-------+ 1 row in set (0.00 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)mysql>3)重啟,主庫創建同步用戶slave
[root@host-10-253-234-32 openssh-8.2p1]# systemctl stop mysql [root@host-10-253-234-32 openssh-8.2p1]# systemctl start mysql [root@host-10-253-234-32 openssh-8.2p1]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 123 Server version: 5.7.31 MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by 'JY19MZslave'; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)mysql> mysql> set password=password('PASSWORD'); mysql> grant all privileges on *.* to root@'%' identified by 'PASSWORD';4)配置從庫my.cnf
[root@host-10-253-234-33 ~]# more /etc/my.cnf [client] socket=/u01/mysql5.7/mysql.sock [mysqld] user=mysql basedir=/u01/mysql5.7 datadir=/u01/mysql5.7/data socket=/u01/mysql5.7/mysql.sock character_set_server=utf8 # 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/Systemdserver_id=2 log_bin=mysql-bin binlog_format=MIXED relay_log_recovery=1#[mysqld_safe] #log-error=/var/log/mariadb/mariadb.log #pid-file=/var/run/mariadb/mariadb.pid log-error=/u01/mysql5.7/log/mysql.err pid-file=/u01/mysql5.7/mysql.pid# # include all files from the config directory # !includedir /etc/my.cnf.d mysql> set global expire_logs_days=15; Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec)mysql> show variables like 'expire_logs_days'; +------------------+-------+ | Variable_name | Value | +------------------+-------+ | expire_logs_days | 15 | +------------------+-------+ 1 row in set (0.00 sec)mysql>5)同步主庫數據
導出源庫數據
6)將主庫導出的數據文件復制到目標庫
[root@host-10-253-234-32 tmp]# scp *.sql root@10.253.234.33:/tmp root@10.253.234.33's password: cloudstore.sql 100% 498KB 51.0MB/s 00:00 szmzyl.sql 100% 682MB 110.9MB/s 00:06 szyzt.sql 100% 548MB 109.8MB/s 00:04 yhucmmg.sql 100% 96MB 119.5MB/s 00:00 zsk.sql 100% 617MB 108.6MB/s 00:057、創建數據庫,并將數導入目標庫
[root@host-10-253-234-33 mysql5.7]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 5.7.31-log MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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> create database cloudstore; Query OK, 1 row affected (0.00 sec)mysql> create database szmzyl; Query OK, 1 row affected (0.00 sec)mysql> create database szyzt; Query OK, 1 row affected (0.01 sec)mysql> create database yhucmmg; Query OK, 1 row affected (0.00 sec)mysql> create database zsk; Query OK, 1 row affected (0.00 sec)mysql> exit Bye [root@host-10-253-234-33 ~]# mysql -u root -p zsk < /tmp/zsk.sql; Enter password: [root@host-10-253-234-33 ~]# ls -l /tmp -rw-r--r--. 1 root root 510339 7月 30 22:26 /tmp/cloudstore.sql -rw-r--r--. 1 root root 714921360 7月 30 21:53 /tmp/szmzyl.sql -rw-r--r--. 1 root root 574854723 7月 30 21:53 /tmp/szyzt.sql -rw-r--r--. 1 root root 100624706 7月 30 21:53 /tmp/yhucmmg.sql -rw-r--r--. 1 root root 647413712 7月 30 21:53 /tmp/zsk.sql [root@host-10-253-234-33 ~]# mysql -u root -p szyzt < /tmp/szyzt.sql; Enter password: [root@host-10-253-234-33 ~]#7)主庫執行:show master status; 記下 Position 和 File 的值
mysql> show master status; +------------------+-----------+--------------+-------------------------------------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+-----------+--------------+-------------------------------------------------+-------------------+ | mysql-bin.000001 | 833188576 | | information_schema,mysql,performance_schema,sys | | +------------------+-----------+--------------+-------------------------------------------------+-------------------+ 1 row in set (0.00 sec)8)配置從庫同步配置
[root@host-10-253-234-33 ~]# systemctl stop mysql [root@host-10-253-234-33 ~]# systemctl start mysql [root@host-10-253-234-33 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.31-log MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> change master to-> master_host='10.253.234.32', #主庫IP-> master_user='slave', #主庫同步用戶 -> master_password='JY19MZslave', #主庫同步密碼-> master_port=3306,-> master_log_file='mysql-bin.000001', #7步查出來的log_file-> master_log_pos=833188576; #7步查出來的log_pos Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql> SHOW SLAVE STATUS; +----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+ | Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version | +----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+ | | 10.253.234.32 | slave | 3306 | 60 | mysql-bin.000001 | 833188576 | host-10-253-234-33-relay-bin.000001 | 4 | mysql-bin.000001 | No | No | | | | | | | 0 | | 0 | 833188576 | 154 | None | | 0 | No | | | | | | NULL | No | 0 | | 0 | | | 0 | | /u01/mysql5.7/data/master.info | 0 | NULL | | 86400 | | | | | | | | 0 | | | | +----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+ 1 row in set (0.00 sec)mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> start slave; Query OK, 0 rows affected (0.00 sec)mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: 10.253.234.32Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 833188576Relay_Log_File: host-10-253-234-33-relay-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: ConnectingSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 833188576Relay_Log_Space: 154Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 1045Last_IO_Error: error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 1Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_Info_File: /u01/mysql5.7/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: 200719 11:52:30Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)ERROR: No query specifiedmysql>9)連接異常檢查,檢查日志,登陸失敗。
[root@host-10-253-234-33 log]# tail -f mysql.err 2020-07-19T03:57:30.125046Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 6, Error_code: 1045 2020-07-19T03:58:30.126947Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 7, Error_code: 1045 2020-07-19T03:59:30.128985Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 8, Error_code: 1045 2020-07-19T04:00:30.130947Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 9, Error_code: 1045 2020-07-19T04:01:30.134093Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 10, Error_code: 1045 2020-07-19T04:02:30.135890Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 11, Error_code: 1045 2020-07-19T04:03:30.139051Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 12, Error_code: 1045 2020-07-19T04:04:30.140825Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 13, Error_code: 1045 2020-07-19T04:05:30.142791Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 14, Error_code: 1045 2020-07-19T04:06:30.144744Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60 retries: 15, Error_code: 1045 2020-07-19T04:07:30.146501Z 3 [Note] Slave I/O thread for channel '': connected to master 'slave@10.253.234.32:3306',replication started in log 'mysql-bin.000001' at position 833188576 登陸失敗 [root@host-10-253-234-33 log]# mysql -u slave -h 10.253.234.32 -p Enter password: ERROR 1045 (28000): Access denied for user 'slave'@'10.253.234.33' (using password: YES)10)重新授權主庫
mysql> grant replication slave on *.* to 'slave'@'%' identified by 'JY19MZslave'; ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock mysql> unlock table; Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)mysql> grant replication slave on *.* to 'slave'@'%' identified by 'JY19MZslave'; Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)mysql>mysql>11)啟動后同步報錯,查看是FUNCTION報錯,需要將FUNCTION導到目標庫。
mysql> start slave; mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.253.234.32Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000007Read_Master_Log_Pos: 65236Relay_Log_File: host-10-253-234-33-relay-bin.000004Relay_Log_Pos: 137222651Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: NoReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 1305Last_Error: Error 'FUNCTION yhucmmg.seq does not exist' on query. Default database: 'yhucmmg'. Query: 'SELECT `yhucmmg`.`seq`(_utf8'SEQ_FILEINFO' COLLATE 'utf8_general_ci')'Skip_Counter: 0Exec_Master_Log_Pos: 970410907Relay_Log_Space: 7093622466Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 1305Last_SQL_Error: Error 'FUNCTION yhucmmg.seq does not exist' on query. Default database: 'yhucmmg'. Query: 'SELECT `yhucmmg`.`seq`(_utf8'SEQ_FILEINFO' COLLATE 'utf8_general_ci')'Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: f1f4a61a-c896-11ea-8c77-fa163eae1a0eMaster_Info_File: /u01/mysql5.7/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Master_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 200726 23:25:54Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)ERROR: No query specifiedmysql>12)導出和導入函數
#主庫導出 [root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases szmzyl -ntdR --triggers=false > /tmp/szmzylf.sql Enter password: [root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases cloudstore -ntdR --triggers=false > /tmp/cloudstoref.sql Enter password: [root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases szyzt -ntdR --triggers=false > /tmp/szyztf.sql Enter password: [root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases yhucmmg -ntdR --triggers=false > /tmp/yhucmmgf.sql Enter password: [root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases zsk -ntdR --triggers=false > /tmp/zskf.sql Enter password: [root@host-10-253-234-32 tmp]# scp *f.sql root@10.253.234.33:/tmp root@10.253.234.33's password: cloudstoref.sql 100% 2399 1.3MB/s 00:00 szmzylf.sql 100% 22KB 13.9MB/s 00:00 szyztf.sql 100% 1358 1.8MB/s 00:00 yhucmmgf.sql 100% 2384 3.1MB/s 00:00 zskf.sql 100% 2333 2.8MB/s 00:00 [root@host-10-253-234-32 tmp]# #備庫導入 [root@host-10-253-234-33 ~]# mysql -u root -p yhucmmg < /tmp/fyhucmmg.log; Enter password: ERROR 1418 (HY000) at line 37: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) [root@host-10-253-234-33 ~]# [root@host-10-253-234-33 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 242 Server version: 5.7.31-log MySQL Community Server (GPL)Copyright (c) 2000, 2020, 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 variables like 'log_bin_trust_function_creators'; +---------------------------------+-------+ | Variable_name | Value | +---------------------------------+-------+ | log_bin_trust_function_creators | OFF | +---------------------------------+-------+ 1 row in set (0.00 sec)mysql> mysql> set global log_bin_trust_function_creators=TRUE; Query OK, 0 rows affected (0.00 sec)mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)#再次導入[root@host-10-253-234-33 ~]# mysql -u root -p szmzyl < /tmp/szmzylf.sql Enter password: [root@host-10-253-234-33 ~]# mysql -u root -p szyzt < /tmp/szyztf.sql Enter password: [root@host-10-253-234-33 ~]# mysql -u root -p yhucmmg < /tmp/yhucmmgf.sql Enter password: [root@host-10-253-234-33 ~]# mysql -u root -p cloudstore < /tmp/cloudstoref.sql Enter password: [root@host-10-253-234-33 ~]# mysql -u root -p zsk < /tmp/zskf.sql Enter password: [root@host-10-253-234-33 ~]#11)再檢查狀態正常
mysql> start slave; mysql> show slave status\G; *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 10.253.234.32Master_User: slaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000012Read_Master_Log_Pos: 576500579Relay_Log_File: host-10-253-234-33-relay-bin.000002Relay_Log_Pos: 9547Relay_Master_Log_File: mysql-bin.000012Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 576500579Relay_Log_Space: 9767Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1Master_UUID: f1f4a61a-c896-11ea-8c77-fa163eae1a0eMaster_Info_File: /u01/mysql5.7/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)ERROR: No query specifiedmysql>12)主庫解鎖,開始數據同步。
mysql> unlock table; Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)4、基它參考
START SLAVE; # 啟動從庫
STOP SLAVE; # 關閉從庫
MASTER_LOG_FILE=‘mysql-bin.000001’,#與主庫File 保持一致
MASTER_LOG_POS=833188576 , #與主庫Position 保持一致
測試同步
主從同步延遲#
在主從同步過程中因為所有的SQL必須都要在從服務器里面執行一遍,但是主服務器如果不斷的有更新操作源源不斷的寫入, 那么一旦有延遲產生, 那么延遲加重的可能性就會原來越大。雖然這個問題又不能完全解決,但是我們可以采取一些措施來緩解。
我們知道因為主服務器要負責更新操作, 他對安全性的要求比從服務器高, 所有有些設置可以修改,比如sync_binlog=1,innodb_flush_log_at_trx_commit = 1 之類的設置,而slave則不需要這么高的數據安全,完全可以講sync_binlog設置為0或者關閉binlog,innodb_flushlog, innodb_flush_log_at_trx_commit 也 可以設置為0來提高sql的執行效率 這個能很大程度上提高效率。另外就是使用比主庫更好的硬件設備作為slave。
總結
以上是生活随笔為你收集整理的mysql主从安装配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis 设计与实现 1:数据库 re
- 下一篇: python爬取智联招聘网_python