2018-05-16 Linux学习

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018-05-16 Linux学习相关的知识,希望对你有一定的参考价值。

17-Day47&48-mysql主从配置

17.1 MySQL主从介绍

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步的
MySQL主从是基于binlog的,主上须开启binlog才能进行主从。

主从过程大致有3个步骤
1)主将更改操作记录到binlog里
2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里
3)从根据relaylog里面的sql语句按顺序执行

主上有一个log dump线程,用来和从的I/O线程传递binlog
从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个SQL线程用来把relaylog里面的sql语句落地

17.2 准备工作

操作过程

    cd /usr/local/src
    wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
    tar zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
    mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql
    cd /usr/local/mysql
    useradd -s /sbin/nologin mysql     (或 useradd -r -m -d /data/mysql -s /sbin/nologin mysql )
    mkdir /data/mysql
    ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
    cp support-files/my-default.cnf /etc/my.cnf
    cp support-files/mysql.server /etc/init.d/mysqld

    vim /etc/my.cnf
        定义 datadir&socket
        [mysqld]
        datadir=/data/mysql
        socket=/tmp/mysql.sock

    vi /etc/init.d/mysqld
        定义 basedir 和 datadir
        basedir=/usr/local/mysql
        datadir=/data/mysql

    chmod 755 /etc/init.d/mysqld

    /etc/init.d/mysqld start

17.3 配置主

主从配置 - 主上操作

安装mysql
修改my.cnf,增加server-id=130和log_bin=aminglinux1
修改完配置文件后,启动或者重启mysqld服务

把mysql库备份并恢复成aming库,作为测试数据
mysqldump -uroot mysql > /tmp/mysql.sql
mysql -uroot -e “create database aming”
mysql -uroot aming < /tmp/mysql.sql

创建用作同步数据的用户
grant replication slave on *.* to ‘repl‘@slave_ip identified by ‘password‘;
flush tables with read lock;
show master status;

操作过程

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=160
log_bin=aminglinux01

[[email protected] ~]# /etc/init.d/mysqld restart

[[email protected] mysql]# cd /data/mysql/
[[email protected] mysql]# ls -lt
总用量 110680
-rw-rw----. 1 mysql mysql 50331648 4月  12 05:01 ib_logfile0
-rw-rw----. 1 mysql mysql 12582912 4月  12 05:01 ibdata1
-rw-rw----. 1 mysql mysql    55008 4月  12 05:01 linux-01.err
-rw-rw----. 1 mysql mysql        6 4月  12 05:01 linux-01.pid
-rw-rw----. 1 mysql mysql       22 4月  12 05:01 aminglinux01.index
-rw-rw----. 1 mysql mysql      120 4月  12 05:01 aminglinux01.000001
drwx------. 2 mysql mysql     4096 4月  11 05:39 zrlog
drwx------. 2 mysql mysql     4096 3月  31 21:32 mysql2
-rw-rw----. 1 mysql mysql       56 3月  26 06:01 auto.cnf
drwx------. 2 mysql mysql     4096 3月  26 05:56 mysql
drwx------. 2 mysql mysql     4096 3月  26 05:56 performance_schema
-rw-rw----. 1 mysql mysql 50331648 3月  26 05:55 ib_logfile1
drwx------. 2 mysql mysql        6 3月  26 05:55 test

[[email protected] mysql]# mysqldump -uroot -paminglinux mysql > /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.

[[email protected] mysql]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.

[[email protected] mysql]# mysql -uroot -paminglinux aming < /tmp/mysql.sql
Warning: Using a password on the command line interface can be insecure.

[[email protected] mysql]# mysql -uroot -paminglinux

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

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+---------------------+----------+--------------+------------------+-------------------+
| File                | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------------+----------+--------------+------------------+-------------------+
| aminglinux01.000001 |   669865 |              |                  |                   |
+---------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

[[email protected] mysql]# ls -lht
总用量 222M
-rw-rw----. 1 mysql mysql 655K 4月  12 06:21 aminglinux01.000001
-rw-rw----. 1 mysql mysql  48M 4月  12 06:19 ib_logfile0
-rw-rw----. 1 mysql mysql  76M 4月  12 06:19 ibdata1
drwx------. 2 mysql mysql 4.0K 4月  12 06:19 aming
drwxr-x---. 2 root  root    19 4月  12 05:09 logs
-rw-rw----. 1 mysql mysql  54K 4月  12 05:01 linux-01.err
-rw-rw----. 1 mysql mysql    6 4月  12 05:01 linux-01.pid
-rw-rw----. 1 mysql mysql   22 4月  12 05:01 aminglinux01.index
drwx------. 2 mysql mysql 4.0K 4月  11 05:39 zrlog
drwx------. 2 mysql mysql 4.0K 3月  31 21:32 mysql2
-rw-rw----. 1 mysql mysql   56 3月  26 06:01 auto.cnf
drwx------. 2 mysql mysql 4.0K 3月  26 05:56 mysql
drwx------. 2 mysql mysql 4.0K 3月  26 05:56 performance_schema
-rw-rw----. 1 mysql mysql  48M 3月  26 05:55 ib_logfile1
drwx------. 2 mysql mysql    6 3月  26 05:55 test

[[email protected] mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysqldump -uroot -paminglinux mysql2 > /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[[email protected] mysql]# mysqldump -uroot -paminglinux test > /tmp/test.sql
Warning: Using a password on the command line interface can be insecure.

17.4 配置从

主从配置 - 从上操作

安装mysql
查看my.cnf,配置server-id=132,要求和主不一样
修改完配置文件后,启动或者重启mysqld服务

把主上aming库同步到从上
可以先创建aming库,然后把主上的/tmp/mysql.sql拷贝到从上,然后导入aming库
mysql -uroot
stop slave;
change master to master_host=‘‘, master_user=‘repl‘, master_password=‘‘, master_log_file=‘‘, master_log_pos=xx,
start slave;
还要到主上执行 unlock tables

查看主从同步是否正常

从上执行mysql -uroot
show slave stauts\G
看是否有
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
还需关注
Seconds_Behind_Master: 0  //为主从延迟的时间
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:

操作过程

[[email protected] ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=165

[[email protected] ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

[[email protected] ~]# scp 192.168.106.160:/tmp/*.sql /tmp/
[email protected]‘s password: 
mysql2.sql                                                        100%  645KB  31.7MB/s   00:00    
zrlog.sql                                                         100%   11KB   5.5MB/s   00:00

[[email protected] ~]# alias ‘mysql=/usr/local/mysql/bin/mysql‘
[[email protected] ~]# alias ‘mysqldump=/usr/local/mysql/bin/mysqldump‘

[[email protected] ~]# mysql -uroot

mysql> create database aming;
Query OK, 1 row affected (0.00 sec)

mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)

mysql> quit

[[email protected] ~]# mysql -uroot mysql2 < /tmp/mysql2.sql 
[[email protected] ~]# mysql -uroot zrlog < /tmp/zrlog.sql 

[[email protected] ~]# mysql -uroot

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host=‘192.168.106.160‘,master_user=‘repl‘,master_password=‘aminglinux111‘,master_log_file=‘aminglinux01.000001‘,master_log_pos=669865;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************

             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes


[[email protected] ~]# mysql -uroot -paminglinux

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

17.5 测试主从同步

几个配置参数

主服务器上
    binlog-do-db=      //仅同步指定的库
    binlog-ignore-db= //忽略指定库
    从服务器上
            replicate_do_db=
            replicate_ignore_db=
            replicate_do_table=
            replicate_ignore_table=
            以上四项不建议使用
    replicate_wild_do_table=   //如aming.%, 支持通配符% 
    replicate_wild_ignore_table=

测试主从

主上 mysql -uroot aming  
select count(*) from db;
truncate table db;
到从上 mysql -uroot aming
select count(*) from db;
主上继续drop table db;
从上查看db表

操作过程

mysql> use aming;
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_aming |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)

mysql> select count(*) user;
+------+
| user |
+------+
| 1 |
+------+
1 row in set (0.00 sec)

以上是关于2018-05-16 Linux学习的主要内容,如果未能解决你的问题,请参考以下文章

向Linus学习,让代码具有good taste

[linux][c/c++]代码片段01

[linux][c/c++]代码片段02

IOS开发-OC学习-常用功能代码片段整理

java SpringRetry学习的代码片段

python 机器学习有用的代码片段