进击的Mysql——Slave环境建立及装备51CTO博客 - 乐橙lc8

进击的Mysql——Slave环境建立及装备51CTO博客

2019-03-06 10:23:51 | 作者: 寻桃 | 标签: 装备,数据库,环境 | 浏览: 106

一)环境拓扑

   今日评论的是mysql集群这一块,资源如下所示:

二)Mysql装置装备

   这儿不逐个赘述,能够挑选源代码编译装置,也能够参阅我之前的一篇博文“懒人“速成——linux LAMP环境。

三)节点装备

主节点:

   1.首先在数据库中树立2个数据库和表:

#service mysqld start
#mysql
mysql>create database www;
mysql>use www;
mysql>create table www(id int);
mysql>insert into www values(1);
mysql> select * from www;

   检查数据:

   同理创立blog数据库和表:

mysql>create database blog;
mysql>use blog;
mysql>create table blog(id int);
mysql>insert into blog values(1);
mysql> select * from blog;


2.修正My.cnf装备文件

vi etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin          //日志为2进制,不需求更改
server-id    =1            //为1就是Master,不需求更改
binlog-do-db=blog          //需求同步的库
binlog-do-db=www           //需求同步的库
binlog-ignore-db=mysql,test,information_schema   //不需求记载日志的数据库名,多个数据库中心用逗号(,)离隔
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
innodb_data_home_dir = /usr/local/mysql/data/      //innodb的表空间方位
innodb_data_file_path = ibdata1:50M:autoextend     //表空间的姓名,开端50M
innodb_log_group_home_dir = /usr/local/mysql/data/
innodb_buffer_pool_size = 256M                     //为体系内存的50-80%
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

   3.重启数据库

[root@Test01 /]# service mysqld restart


Stopping mysqld:  [  OK  ]
Starting mysqld:  [  OK  ]

4.创立有权限的账号,让Slave数据库拜访主数据库


[root@Test01 /]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.69-log Source distribution
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>



mysql> grant replication slave on *.* to repl@"192.168.1.26" identified by 123456;
Query OK, 0 rows affected (0.00 sec)



mysql>flush privileges;
;

留意:格局:GRANT REPLICATION SLAVE ON *.* TO '帐号'@'从服务器IP或主机名' IDENTIFIED BY '暗码';

 5.备份Master数据库


mysql> flush tables with read lock;  //不要退出这个终端,不然这个锁就不收效了。
一起获得快照,并记载日志和偏移量:
mysql> show master status;
+------------------+----------+--------------+-------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB              |
+------------------+----------+--------------+-------------------------------+
| mysql-bin.000001 |      196 | blog,www     | mysql,test,information_schema |
+------------------+----------+--------------+-------------------------------+
1 row in set (0.00 sec)

6.敞开另一个终端,对主数据库做快照


[root@Test01 mysql]#cd /var/lib/mysql/
[root@Test01 mysql]#tar -zcvf backup.tar.gz www blog

 7.库解锁


mysql> unlock tables;

从节点:

   1.装置mysql,办法同上,不在赘述。

   2.修正My.cnf装备文件


[root@Test02 mysql]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin
server-id  = 2
#master-host     =   192.168.1.10
#master-user     =   root
#master-password =   584911644
#master-port     =  3306
#master-log-pos =   196
#master-log-file = mysql-bin.000001
#master-connect-retry=60
replicate-do-db=www        //通知slave只做www数据库的更新
replicate-do-db=blog       //通知slave只做blog数据库的更新
log-slave-updates
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
"/etc/my.cnf" 23L, 551C

3.把之前主数据库紧缩的数据拷贝到对应方位

我这儿用的是SecureFX,当然,办法许多,也不细说了,可谓是条条大路通罗马。

4.发动数据库


[root@Test02 mysql]# service mysqld start


Starting mysqld:  [  OK  ]

5.装备,发动Slave

留意:Slave_IO_Running:是否要从 Master Server 复製 Binary Log 材料,有必要为 Yes。Slave_SQL_Running:是否要履行从 Master Server 复製过来的 Binary Log 材料,有必要为 Yes。

mysql> slave stop;
Query OK, 0 rows affected (0.00 sec)
mysql> change master to master_host=192.168.1.10,master_user=repl,master_password=123456,master_log_file=mysql-bin.000001,master_log_pos=196;
Query OK, 0 rows affected (0.04 sec)
mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.10
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 339
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 394
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: www,blog
          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: 339
              Relay_Log_Space: 550
              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:
1 row in set (0.00 sec)

四)后期测验

主节点-数据添加:


mysql>  insert into www values(2);
Query OK, 1 row affected (0.00 sec)
mysql> select * from www;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

从节点数据同步验证:


mysql> select * from www;
+------+
| id   |
+------+
|    1 |
|    2 |
+------+
2 rows in set (0.00 sec)

主节点-数据删去:


mysql> delete from www  where id =2;
Query OK, 1 row affected (0.04 sec)
mysql> select * from www;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

从节点数据同步验证:


mysql> select * from www;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)


总述:

   Test02,从数据库上能做到同步数据库的更新操作,现在是WWW和blog两张表。当然,这儿仅仅抛砖引玉的效果,我们能够触类旁通,依据自己实践需求,做出最好的环境搭建和装备。

   最近在弄RHCS和keepalived,闲来有空就记载几笔数据库slave装备,便利我们学习和自己的堆集。


版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表乐橙lc8立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章