首页 > 代码库 > mysql 主从同步
mysql 主从同步
第一章 安装mysql
mysql安装包:
MySQL-server-5.6.11-1.rhel5.x86_64.rpm MySQL-client-5.6.11-1.rhel5.x86_64.rpm |
安装mysql:
[root@SALT1 software]# rpm -ivh MySQL-server-5.6.11-1.rhel5.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-server ########################################### [100%] 2017-03-30 17:10:00 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-03-30 17:10:00 24684 [Note] InnoDB: The InnoDB memory heap is disabled 2017-03-30 17:10:00 24684 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-03-30 17:10:00 24684 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-03-30 17:10:00 24684 [Note] InnoDB: Using Linux native AIO 2017-03-30 17:10:00 24684 [Note] InnoDB: Using CPU crc32 instructions 2017-03-30 17:10:00 24684 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-03-30 17:10:00 24684 [Note] InnoDB: Completed initialization of buffer pool 2017-03-30 17:10:00 24684 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created! 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB 2017-03-30 17:10:00 24684 [Note] InnoDB: Database physically writes the file full: wait... 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB 2017-03-30 17:10:00 24684 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB 2017-03-30 17:10:01 24684 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0 2017-03-30 17:10:01 24684 [Warning] InnoDB: New log files created, LSN=45781 2017-03-30 17:10:01 24684 [Note] InnoDB: Doublewrite buffer not found: creating new 2017-03-30 17:10:01 24684 [Note] InnoDB: Doublewrite buffer created 2017-03-30 17:10:01 24684 [Note] InnoDB: 128 rollback segment(s) are active. 2017-03-30 17:10:01 24684 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-03-30 17:10:01 24684 [Note] InnoDB: Foreign key constraint system tables created 2017-03-30 17:10:01 24684 [Note] InnoDB: Creating tablespace and datafile system tables. 2017-03-30 17:10:01 24684 [Note] InnoDB: Tablespace and datafile system tables created. 2017-03-30 17:10:01 24684 [Note] InnoDB: Waiting for purge to start 2017-03-30 17:10:01 24684 [Note] InnoDB: 5.6.11 started; log sequence number 0 A random root password has been set. You will find it in ‘/root/.mysql_secret‘. 2017-03-30 17:10:01 24684 [Note] Binlog end 2017-03-30 17:10:01 24684 [Note] InnoDB: FTS optimize thread exiting. 2017-03-30 17:10:01 24684 [Note] InnoDB: Starting shutdown... 2017-03-30 17:10:03 24684 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2017-03-30 17:10:03 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-03-30 17:10:03 24707 [Note] InnoDB: The InnoDB memory heap is disabled 2017-03-30 17:10:03 24707 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2017-03-30 17:10:03 24707 [Note] InnoDB: Compressed tables use zlib 1.2.3 2017-03-30 17:10:03 24707 [Note] InnoDB: Using Linux native AIO 2017-03-30 17:10:03 24707 [Note] InnoDB: Using CPU crc32 instructions 2017-03-30 17:10:03 24707 [Note] InnoDB: Initializing buffer pool, size = 128.0M 2017-03-30 17:10:03 24707 [Note] InnoDB: Completed initialization of buffer pool 2017-03-30 17:10:03 24707 [Note] InnoDB: Highest supported file format is Barracuda. 2017-03-30 17:10:03 24707 [Note] InnoDB: 128 rollback segment(s) are active. 2017-03-30 17:10:03 24707 [Note] InnoDB: Waiting for purge to start 2017-03-30 17:10:03 24707 [Note] InnoDB: 5.6.11 started; log sequence number 1625977 2017-03-30 17:10:03 24707 [Note] Binlog end 2017-03-30 17:10:03 24707 [Note] InnoDB: FTS optimize thread exiting. 2017-03-30 17:10:03 24707 [Note] InnoDB: Starting shutdown... 2017-03-30 17:10:05 24707 [Note] InnoDB: Shutdown completed; log sequence number 1625987
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in ‘/root/.mysql_secret‘.
You must change that password on your first connect, no other statement but ‘SET PASSWORD‘ will be accepted. See the manual for the semantics of the ‘password expired‘ flag.
Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database. This is strongly recommended for production servers.
See the manual for more instructions.
Please report any problems with the /usr/bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and will be used by default by the server when you start it. You may edit this file to change server settings
[root@SALT1 software]# |
安装mysql client端:
[root@SALT1 software]# rpm -ivh MySQL-client-5.6.11-1.rhel5.x86_64.rpm Preparing... ########################################### [100%] 1:MySQL-client ########################################### [100%] |
第二章 配置mysql
拷贝配置文件到/etc目录下:
[root@SALT1 software]# cd /usr/share/mysql/ [root@SALT1 mysql]# cp my-default.cnf /etc/my.cnf |
修改配置文件:在[mysqld]下面添加如下红色标注的配置
[root@SALT1 mysql]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect=‘SET NAMES utf8‘ max_allowed_packet=256M max_connections=1000 … |
保存退出
root密码重置
查看mysql状态:
[root@SALT1 mysql]# /etc/init.d/mysql status ERROR! MySQL is not running |
修改配置文件:在[mysqld]下面添加如下红色标注的配置
[root@SALT1 mysql]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect=‘SET NAMES utf8‘ max_allowed_packet=256M max_connections=1000 skip-grant-tables … |
启动mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
重置root密码
[root@SALT1 mysql]# mysql -uroot -p Enter password: 【直接回车就可以】 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.11 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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 mysql; 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> update user set password=password(‘rootroot‘) where user=‘root‘; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> exit |
修改my.cnf,删掉skip-grant-tables,开启权限认证
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! [root@SALT1 software]# vi /etc/my.cnf … [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect=‘SET NAMES utf8‘ max_allowed_packet=256M max_connections=1000 … |
保存退出,启动mysql
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL. SUCCESS! |
修改root密码:
[root@SALT1 mysql]# 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.6.11
Copyright (c) 2000, 2013, 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 mysql; ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> set password=password(‘rootroot‘); #密码可根据实际情况改变 Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> exit Bye |
上述步骤主库和从库都需要操作
第三章 mysql主从同步配置
主库配置:
停止mysql
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! |
增加主库配置:增加红色标注的配置
[root@SALT1 mysql]# vi /etc/my.cnf [mysqld] …. datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect=‘SET NAMES utf8‘ max_allowed_packet=256M max_connections=1000 log-bin=mysql-bin #启用二进制日志 binlog_format=mixed #指定二进制日志格式 server-id=163 #指定服务器ID(建议使用ip地址的主机位) …. |
启动mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
创建同步账号:
[root@SALT1 software]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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 ‘password123‘; Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 398 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> mysql> mysql> mysql> mysql> exit Bye [root@SALT1 software]# |
现在主库配置完毕,记录下File和Position的值,此时主库不要做任何更改!!!
GRANT REPLICATION SLAVE ON *.* to ‘slave‘@‘%‘identified by ‘password123‘; 命令解析:
创建一个slave的账号,密码为password123,给予该账号对所有database的replication权限,%代表任意主机都可以用该账号登录到mysql,
从库配置:
停止mysql
[root@SALT1 mysql]# /etc/init.d/mysql stop Shutting down MySQL.. SUCCESS! |
增加从库配置:增加红色标注的配置:
[root@SALT1 mysql]# vi /etc/my.cnf [mysqld] …. datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock lower_case_table_names=1 event_scheduler=ON character_set_server=utf8 init_connect=‘SET NAMES utf8‘ max_allowed_packet=256M max_connections=1000 log-bin=mysql-bin #启用二进制日志 binlog_format=mixed #指定二进制日志格式 server-id=164 #指定服务器ID(建议使用ip地址的主机位) …. |
启动mysql:
[root@SALT1 mysql]# /etc/init.d/mysql start Starting MySQL SUCCESS! |
设置从服务器:
[root@SALT1 software]# 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.6.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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> change master to master_host=‘192.168.2.163‘,master_user=‘slave‘,master_password=‘password123‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=398; Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> start slave; #启动同步 Query OK, 0 rows affected (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 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Waiting for master to send event | 192.168.2.163 | slave | 3306 | 60 | mysql-bin.000001 | 398 | SALT1-relay-bin.000002 | 283 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 398 | 456 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 163 | 0490786c-1519-11e7-a1c4-000c29623e16 | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ 1 row in set (0.00 sec)
mysql> |
参数解释:MASTER_HOST : 设置要连接的主服务器的ip地址
MASTER_USER : 设置要连接的主服务器的用户名
MASTER_PASSWORD : 设置要连接的主服务器的密码
MASTER_LOG_FILE : 设置要连接的主服务器的bin日志的日志名称,即主机查询到的File的值;
MASTER_LOG_POS : 设置要连接的主服务器的bin日志的记录位置,即主机查询到的Position的值,(这里注意,最后一项不需要加引号。否则配置失败)
将结果复制到notepad中:
只要这两个值为YES就说明主从配置成功。
测试:在主库创建一个db,查看备库是否同步。
主库163:
[root@SALT1 software]# 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.11-log MySQL Community Server (GPL)
Copyright (c) 2000, 2013, 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 yqqdb character set utf8; Query OK, 1 row affected (0.00 sec)
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yqqdb | +--------------------+ 5 rows in set (0.00 sec)
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 593 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
mysql> Ctrl-C -- exit! Aborted [root@SALT1 software]# |
查看从库164:
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 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ | Waiting for master to send event | 192.168.2.163 | slave | 3306 | 60 | mysql-bin.000001 | 593 | SALT1-relay-bin.000002 | 478 | mysql-bin.000001 | Yes | Yes | | | | | | | 0 | | 0 | 593 | 651 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 163 | 0490786c-1519-11e7-a1c4-000c29623e16 | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for the slave I/O thread to update it | 86400 | | | | | | | | 0 | +----------------------------------+---------------+-------------+-------------+---------------+------------------+---------------------+------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+-----------------------------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+ 1 row in set (0.00 sec)
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | yqqdb | +--------------------+ 5 rows in set (0.01 sec)
mysql> |
从库可以看到主库创建的yqqdb,说明主从配置成功。
本文出自 “不二兔” 博客,请务必保留此出处http://buertu.blog.51cto.com/7159900/1911855
mysql 主从同步