mysql切换到从节点_Linux 搭建Mysql主从节点复制

Linux环境 Centos 6.6 64位

准备两台服务器,搭建一主一从,实现Mysql的读写分离和数据备份

主节点 192.168.43.10 leader

从节点 192.168.43.20 db

安装包 mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz(该包含有NDB和Mysql软件)

MySQL主从复制的优点:

1、 如果主服务器出现问题, 可以快速切换到从服务器提供的服务,保证高可用性

2、 可以在从服务器上执行查询操作, 降低主服务器的访问压力

3、 可以在从服务器上执行备份, 以避免备份期间影响主服务器的服务

1.现在两台服务器上安装好版本一样的Mysql,这里演示Leader端的安装,db跳过

1.1解压安装包到路径/usr/local/mysql

[root@leader vm]# tar -zvxf mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64.tar.gz

[root@leader vm]# mv mysql-cluster-gpl-7.6.11-linux-glibc2.12-x86_64 /usr/local/mysql

1.2创建mysql用户和组以及存放数据的路径

[root@leader mysql]# groupadd mysql

[root@leader mysql]# useradd -r -g mysql mysql

1.3进入到mysql目录,执行添加MySQL配置的操作

创建my.cnf文件,加入如下内容

[root@leader ~]#vi /etc/my.cnf

[mysqld]

basedir=/usr/local/mysql

datadir=/usr/local/mysql/data

port=3306

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#skip-grant-tables

socket = /tmp/mysql.sock

character-set-server = utf8

skip-name-resolve

1.4授权相关路径给用户Mysql

[root@leader mysql]# chown -R mysql .

[root@leader mysql]#chgrp -R mysql .

[root@leader mysql]# chown -R root .

[root@leader mysql]# mkdir data

[root@leader mysql]# chown -R mysql data

1.5初始化数据(在mysql/bin或者mysql/scripts下有个 mysql_install_db 可执行文件初始化数据库),进入mysql/bin或者mysql/scripts目录下,执行下面命令

[root@leader bin]#./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

2019-11-23T12:10:15.096248Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2019-11-23T12:10:15.096315Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.

2019-11-23T12:10:15.096320Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.

2019-11-23T12:10:16.643511Z 0 [Warning] InnoDB: New log files created, LSN=45790

2019-11-23T12:10:16.904240Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2019-11-23T12:10:17.034430Z 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: 36285a17-0dea-11ea-908d-000c298eae92.

2019-11-23T12:10:17.039576Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2019-11-23T12:10:17.042792Z 1 [Note] A temporary password is generated for root@localhost: 8Ns2os:NFde-

1.6添加mysqld服务

[root@leader bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@leader bin]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

[root@leader bin]# chmod 775 /etc/init.d/mysql

[root@leader bin]# chkconfig --add mysqld

[root@leader bin]# chkconfig --level 2345 mysqld on

[root@leader bin]#chown mysql.mysql -R /usr/local/mysql

[root@leader ~]# ln  -s /usr/local/mysql/bin/mysql  /usr/bin--添加mysql命令

1.7 启动mysqld服务,并登陆mysql

[root@leader ~]# service mysqld start

Starting MySQL.Logging to '/usr/local/mysql/data/leader.err'.

. SUCCESS!

[root@leader ~]# clear

[root@leader ~]# service mysqld status

SUCCESS! MySQL running (22899)

[root@leader ~]# mysql -uroot -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.27-ndb-7.6.11-cluster-gpl

Copyright (c) 2000, 2019, 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> set password=password('root');

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'duan' with grant option;

Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> Flush privileges;

Query OK, 0 rows affected (0.01 sec)

2.先登录主机 leader,在主服务器上,设置一个从数据库的账户,使用REPLICATION SLAVE(从复制)赋予权限,

mysql>GRANT REPLICATION SLAVE ON *.* TO 'db'@'192.168.43.20' IDENTIFIED BY '123456'

2.1打开主机leader的my.cnf,输入如下:(修改主数据库的配置文件my.cnf,开启BINLOG,并设置server-id的值,修改之后必须重启mysql服务)

server-id =1 #主机标示,整数

log_bin = /var/log/mysql/mysql-bin.log #确保此文件可写,开启bin-log

read-only =0 #主机,读写都可以

binlog-do-db =test #需要备份数据,多个写多行

binlog-ignore-db =mysql #不需要备份的数据库,多个写多行

2.1创建binlog路径并授权mysql,重启mysqld服务

[root@leader log]#mkdir mysql

[root@leader log]# chown -R mysql.mysql /var/log/mysql

2.3现在可以停止主数据的的更新操作,并生成主数据库的备份,我们可以通过mysqldump导出数据到从数据库,当然了,你也可以直接用cp命令将数据文件复制到从数据库去,注意在导出数据之前先对主数据库进行READ LOCK,以保证数据的一致性:

mysql> create database test;

Query OK, 1 row affected (0.05 sec)

mysql> use test;

Database changed

mysql> create table test_aaa as select * from user;

ERROR 1146 (42S02): Table 'test.user' doesn't exist

mysql>create table test_aaa as select * from mysql.user;

Query OK, 5 rows affected (0.04 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

[root@leader bin]#/usr/local/mysql/bin/mysqldump -p3306 -uroot -p test>/data/backup/test.sql

mysqldump: [Warning] Using a password on the command line interface can be insecure.

Enter password:

将备份文件传到从节点,在从节点创建test库进行恢复;

[root@db bin]# mysql -uroot -proot

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, 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 test;

Query OK, 1 row affected (0.07 sec)

mysql> exit

Bye

[root@db bin]# mysql -uroot -proot test

mysql: [Warning] Using a password on the command line interface can be insecure.

2.4得到主服务器当前二进制日志名和偏移量,这个操作的目的是为了在从数据库启动后,从这个点开始进行数据的恢复。

mysql>show master status;

+------------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+------------------+----------+--------------+------------------+-------------------+

| mysql-bin.000001 | 4383 | test | mysql | |

+------------------+----------+--------------+------------------+-------------------+

1 row in set (0.00 sec)

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

2.5修改从数据库的my.cnf,增加server-id参数。打开从机db的my.cnf,输入(修改之后必须重启mysql服务)

server-id = 2

relay-log=mysql-relay #打开relaylog

binlog-do-db =test #需要备份数据,多个写多行

binlog-ignore-db =mysql #不需要备份的数据库,多个写多行

2.6登陆从库,指定复制使用的用户,主数据库服务器的ip,端口以及开始执行复制日志的文件和位置

[root@db local]# mysql -uroot -proot

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, 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 = '192.168.43.10',

-> master_user = 'db',

-> master_password = '123456',

-> master_log_file = 'mysql-bin.000003',

-> master_log_pos = 154;

Query OK, 0 rows affected, 2 warnings (0.10 sec)

mysql>start slave;

Query OK, 0 rows affected (0.06 sec)

mysql> show slave status\G;

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.43.10

Master_User: db

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000003

Read_Master_Log_Pos: 154

Relay_Log_File: mysql-relay.000002

Relay_Log_Pos: 320

Relay_Master_Log_File: mysql-bin.000003

Slave_IO_Running: Yes    #为Yes表示配置成功

Slave_SQL_Running: Yes  #为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: 154

Relay_Log_Space: 523

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: 1

Master_UUID: 36285a17-0dea-11ea-908d-000c298eae92

Master_Info_File: /usr/local/mysql/data/master.info

SQL_Delay: 0

SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

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

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

1 row in set (0.01 sec)

ERROR:

No query specified

3.测试主从服务器是否能同步

主节点

[root@leader data]# mysql -uroot -proot

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.7.27-ndb-7.6.11-cluster-gpl-log MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, 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 test;

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> create table test1 as select * from mysql.user;

Query OK, 5 rows affected (0.13 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql>create table test2 as select * from mysql.user;

Query OK, 5 rows affected (0.09 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test3 as select * from mysql.user;

Query OK, 5 rows affected (0.06 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql>create table test4 as select * from mysql.user;

Query OK, 5 rows affected (2.85 sec)

Records: 5 Duplicates: 0 Warnings: 0

mysql> create table test5 as select * from mysql.user;

Query OK, 5 rows affected (0.07 sec)

Records: 5 Duplicates: 0 Warnings: 0

从节点:

[root@db local]# mysql -uroot -proot

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 5

Server version: 5.7.27-ndb-7.6.11-cluster-gpl MySQL Cluster Community Server (GPL)

Copyright (c) 2000, 2019, 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 |

| mysql |

| performance_schema |

| sys |

| test |

+--------------------+

5 rows in set (0.00 sec)

mysql> use test;

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_test |

+----------------+

| test1 |

| test2 |

| test3 |

| test4 |

| test5 |

| test_aaa |

+----------------+

6 rows in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值