MySql common tools, logs, master-slave replication

MySql commonly used tool

1, connection options

mysql -h 127.0.0.1 -P 3306 -u root -p
mysql -h127.0.0.1 -P3306 -uroot -p2143

2, execution options

此选项可以在Mysql客户端执行SQL语句,而不用连接到MySQL数据库再执行,对于一些批处理脚本,这种方式尤
其方便。
mysql -uroot -p2143 db01 -e "select * from tb_book";

3、mysqladmin

mysqladmin 是一个执行管理操作的客户端程序。可以用它来检查服务器的配置和当前状态、创建并删除数据库等。
mysqladmin --help 指令查看帮助文档
示例 :
mysqladmin -uroot -p2143 create 'test01'; 
mysqladmin -uroot -p2143 drop 'test01';
mysqladmin -uroot -p2143 version;

4、mysqlbinlog

由于服务器生成的二进制日志文件以二进制格式保存,所以如果想要检查这些文本的文本格式,就会使用到
mysqlbinlog 日志管理工具。
mysqlbinlog [options] log-files1 log-files2 ...
选项:
-d, --database=name : 指定数据库名称,只列出指定的数据库相关操作。
-o, --offset=# : 忽略掉日志中的前n行命令。
-r,--result-file=name : 将输出的文本格式日志输出到指定文件。
-s, --short-form : 显示简单格式, 省略掉一些信息。
--start-datatime=date1 --stop-datetime=date2 : 指定日期间隔内的所有日志。
--start-position=pos1 --stop-position=pos2 : 指定位置间隔内的所有日志。

5、mysqldump

mysqldump 客户端工具用来备份数据库或在不同数据库之间进行数据迁移。备份内容包含创建表,及插入表的
SQL语句。
mysqldump [options] db_name [tables]
mysqldump [options] --database/-B db1 [db2 db3...]
mysqldump [options] --all-databases/-A
输出内容选项
参数:
--add-drop-database 在每个数据库创建语句前加上 Drop database 语句
--add-drop-table 在每个表创建语句前加上 Drop table 语句 , 默认开启 ; 不开启 (--
skip-add-drop-table)
-n, --no-create-db 不包含数据库的创建语句
-t, --no-create-info 不包含数据表的创建语句
-d --no-data 不包含数据
-T, --tab=name 自动生成两个文件:一个.sql文件,创建表结构的语句;
一个.txt文件,数据文件,相当于select into outfile 
示例 :
mysqldump -uroot -p2143 db01 tb_book --add-drop-database --add-drop-table > a
mysqldump -uroot -p2143 -T /tmp test city

Here Insert Picture Description
Here Insert Picture Description
Export the database table file table structure (.sql) and data (.txt) derived separately.
Here Insert Picture Description
Import command top export txt data files.
Here Insert Picture Description

6、mysqlimport/source

mysqlimport 是客户端数据导入工具,用来导入mysqldump 加 -T 参数后导出的文本文件。
mysqlimport -uroot -p2143 test /tmp/city.txt 
如果需要导入sql文件,可以使用mysql中的source 指令 :
source /root/tb_book.sql 

Source sql import file (table structure and table data)
Here Insert Picture Description
. 7, the mysqlshow

mysqlshow 客户端对象查找工具,用来很快地查找存在哪些数据库、数据库中的表、表中的列或者索引。
mysqlshow [options] [db_name [table_name [col_name]]]
--count 显示数据库及表的统计信息(数据库,表 均可以不指定)
-i 显示指定数据库或者指定表的状态信息
#查询每个数据库的表的数量及表中记录的数量
mysqlshow -uroot -p2143 --count
#查询test库中每个表中的字段书,及行数
mysqlshow -uroot -p2143 test --count
#查询test库中book表的详细情况
mysqlshow -uroot -p2143 test book --count

You can view the entire database has many tables as well as how much data
Here Insert Picture Description
to see how much of a database table how much data
Here Insert Picture Description
specific to a particular table
Here Insert Picture Description
details of a table
Here Insert Picture Description

Mysql log

在任何一种数据库中,都会有各种各样的日志,记录着数据库工作的方方面面,以帮助数据库管理员追踪数据库曾
经发生过的各种事件。MySQL 也不例外,在 MySQL 中,有 4 种不同的日志,分别是错误日志、二进制日志
(BINLOG 日志)、查询日志和慢查询日志,这些日志记录着数据库在不同方面的踪迹。

1, the error log

错误日志是 MySQL 中最重要的日志之一,它记录了当 mysqld 启动和停止时,以及服务器在运行过程中发生任何
严重错误时的相关信息。当数据库出现任何故障导致无法正常使用时,可以首先查看此日志。
该日志是默认开启的 , 默认存放目录为 mysql 的数据目录(var/lib/mysql), 默认的日志文件名为
hostname.err(hostname是主机名)。
查看日志位置指令 :
	show variables like 'log_error%'; 
查看日志内容 :
	tail -f /var/lib/mysql/xaxh-server.err 

The error log is enabled by default. Check the error log storage directory
Here Insert Picture Description
2, the binary log
binary log: contains additions and deletions to the log, the log does not contain the query.
Query Log: Contains all client CRUD operation log

二进制日志(BINLOG)记录了所有的 DDL(数据定义语言)语句和 DML(数据操纵语言)语句,但是不包括数
据查询语句。此日志对于灾难时的数据恢复起着极其重要的作用,MySQL的主从复制, 就是通过该binlog实现
的。
二进制日志,默认情况下是没有开启的,需要到MySQL的配置文件中开启,并配置MySQL日志的格式。
配置文件位置 : /usr/my.cnf
日志存放位置 : 配置时,给定了文件名但是没有指定路径,日志默认写入Mysql的数据目录。
#配置开启binlog日志, 日志的文件前缀为 mysqlbin -----> 生成的文件名如 :
	mysqlbin.000001,mysqlbin.000002
	log_bin=mysqlbin
#配置二进制日志的格式
	binlog_format=STATEMENT
日志格式
STATEMENT
该日志格式在日志文件中记录的都是SQL语句(statement),每一条对数据进行修改的SQL都会记录在日志文件
中,通过Mysql提供的mysqlbinlog工具,可以清晰的查看到每条语句的文本。主从复制的时候,从库(slave)会
将日志解析为原文本,并在从库重新执行一次。
ROW
该日志格式在日志文件中记录的是每一行的数据变更,而不是记录SQL语句。比如,执行SQL语句 : update
tb_book set status='1' , 如果是STATEMENT 日志格式,在日志中会记录一行SQL文件; 如果是ROW,由于是对全
表进行更新,也就是每一行记录都会发生变更,ROW 格式的日志中会记录每一行的数据变更。
MIXED
这是目前MySQL默认的日志格式,即混合了STATEMENT 和 ROW两种格式。默认情况下采用STATEMENT,但是在
一些特殊情况下采用ROW来进行记录。MIXED 格式能尽量利用两种模式的优点,而避开他们的缺点。

Logreader

由于日志以二进制方式存储,不能直接读取,需要用mysqlbinlog工具来查看,语法如下 :
mysqlbinlog log-file;

View STATEMENT format of the log
Here Insert Picture Description

mysqlbin.index : 该文件是日志索引文件 , 记录日志的文件名,表示包含的二进制文件名;
mysqlbing.000001 :日志文件
查看日志内容 :
mysqlbinlog mysqlbing.000001;

Here Insert Picture Description
View ROW format of the log

#配置开启binlog日志, 日志的文件前缀为 mysqlbin -----> 生成的文件名如 :
	mysqlbin.000001,mysqlbin.000002
	log_bin=mysqlbin
#配置二进制日志的格式
	binlog_format=ROW
如果日志格式是 ROW , 直接查看数据 , 是查看不懂的 ; 可以在mysqlbinlog 后面加上参数 -vv
	mysqlbinlog -vv mysqlbin.000002

Here Insert Picture Description
Log Delete

对于比较繁忙的系统,由于每天生成日志量大 ,这些日志如果长时间不清楚,将会占用大量的磁盘空间。下面我们
将会讲解几种删除日志的常见方法 :
方式一
通过 Reset Master 指令删除全部 binlog 日志,删除之后,日志编号,将从 xxxx.000001重新开始 。
查询之前 ,先查询下日志文件 :

Here Insert Picture Description
Delete the log command: Reset Master
Here Insert Picture Description

方式二
执行指令  purge master logs to 'mysqlbin.******' ,该命令将删除  ****** 编号之前的所有日志。
方式三
执行指令  purge master logs before 'yyyy-mm-dd hh24:mi:ss' ,该命令将删除日志为 "yyyy-mm-dd
hh24:mi:ss" 之前产生的所有日志 。
方式四
Reset Master 1
设置参数 --expire_logs_days=# ,此参数的含义是设置日志的过期天数, 过了指定的天数后日志将会被自动删
除,这样将有利于减少DBA 管理日志的工作量。
配置如下 :

Here Insert Picture Description
3, the query log
binary log: contains additions and deletions to the log, the log does not contain the query.
Query Log: Contains all client CRUD operation log

查询日志中记录了客户端的所有操作语句,而二进制日志不包含查询数据的SQL语句。
默认情况下, 查询日志是未开启的。如果需要开启查询日志,可以设置以下配置 :
#该选项用来开启查询日志 , 可选值 : 0 或者 1 ; 0 代表关闭, 1 代表开启
	general_log=1
#设置日志的文件名 , 如果没有指定, 默认的文件名为 host_name.log
	general_log_file=file_name
在 mysql 的配置文件 /usr/my.cnf 中配置如下内容 :

Here Insert Picture Description
After configuration is complete, do the following databases:

select * from tb_book;
select * from tb_book where id = 1;
update tb_book set name = 'lucene入门指南' where id = 5;
select * from tb_book where id < 8;

Here Insert Picture Description
4, slow query log

慢查询日志记录了所有执行时间超过参数 long_query_time 设置值并且扫描记录数不小于
min_examined_row_limit 的所有的SQL语句的日志。long_query_time 默认为 10 秒,最小为 0, 精度可以到微秒。
文件位置和格式
慢查询日志默认是关闭的 。可以通过两个参数来控制慢查询日志 :
# 该参数用来控制慢查询日志是否开启, 可取值: 1 和 0 , 1 代表开启, 0 代表关闭
	slow_query_log=1
# 该参数用来指定慢查询日志的文件名
	slow_query_log_file=slow_query.log
# 该选项用来配置查询的时间限制, 超过这个时间将认为值慢查询, 将需要进行日志记录, 默认10s
	long_query_time=10

Log reading
and error logs, query logs, like, slow query logging format is plain text that can be read directly.
1) the query value of long_query_time.
Here Insert Picture Description
Length of the implementation of 26.77s, over 10s, will be recorded in the slow query log file.
2) slow query log file to see
Here Insert Picture Description
if the slow query log a lot of content, directly view the documents, more trouble, this time by means of mysql comes mysqldumpslow tool to classify summary of the slow query log
Here Insert Picture Description

Mysql replication

Refers to the replication of the primary database DDL and DML operation by binary log transmitted from the library server, and then re-executes the library from these logs (also called redo), so that the data from the database and the main database synchronized.
MySQL supports one of the main library at the same time from the library to copy more than one, from the library while the other can be used as the main library from the server, to achieve chain replication.
Here Insert Picture Description

从上层来看,复制分成三步:
Master 主库在事务提交时,会把数据变更作为时间 Events 记录在二进制日志文件 Binlog 中。
主库推送二进制日志文件 Binlog 中的日志事件到从库的中继日志 Relay Log 。
slave重做中继日志中的事件,将改变反映它自己的数据。
复制优势
主库出现问题,可以快速切换到从库提供服务。
可以在从库上执行查询操作,从主库中更新,实现读写分离,降低主库的访问压力。
可以在从库中执行备份,以避免备份期间影响主库的服务。

Step build
first turn off the firewall servers 2 iptables STOP-Service;
master
. 1) in the master configuration file (/usr/my.cnf), the following configuration:

#mysql 服务ID,保证整个集群环境中唯一
server-id=1
#mysql binlog 日志的存储路径和文件名
log-bin=/var/lib/mysql/mysqlbin
#错误日志,默认已经开启
#log-err
#mysql的安装目录
#basedir
#mysql的临时目录
#tmpdir
#mysql的数据存放目录
#datadir
#是否只读,1 代表只读, 0 代表读写
read-only=0
#忽略的数据, 指不需要同步的数据库
binlog-ignore-db=mysql
#指定同步的数据库
#binlog-do-db=db01

2) After the execution is complete, need to restart Mysql:

service mysql restart ;

3) Create an account sync data and resource authorization:

grant replication slave on *.* to 'itcast'@'192.168.192.131' identified by 'itcast';
flush privileges;

4) View master status: show master status;
Here Insert Picture Description

字段含义:
File : 从哪个日志文件开始推送日志文件
Position : 从哪个位置开始推送日志
Binlog_Ignore_DB : 指定不需要同步的数据库

slave
. 1) at the slave side configuration file, the following configuration:

#mysql服务端ID,唯一
server-id=2
#指定binlog日志
log-bin=/var/lib/mysql/mysqlbin

After 2) is finished, need to restart Mysql: MySQL the restart-Service;
. 3) execute the following instructions:

指定当前从库对应的主库的IP地址,用户名,密码,从哪个日志文件开始的那个位置开始同步推送日志。
change master to master_host= '192.168.192.130', master_user='itcast',
master_password='itcast', master_log_file='mysqlbin.000001', master_log_pos=413;

4) Open synchronous operation

start slave;
show slave status;

That two of which under normal figure is yes basically nothing issue.
Here Insert Picture Description
5) to stop synchronizing operation stop slave;
verify synchronization
1) creating a database in the primary database, creating tables, and inserting data:

create database db01;
user db01;
create table user(
id int(11) not null auto_increment,
name varchar(50) not null,
sex varchar(1),
primary key (id)
)engine=innodb default charset=utf8;
insert into user(id,name,sex) values(null,'Tom','1');
insert into user(id,name,sex) values(null,'Trigger','0');
insert into user(id,name,sex) values(null,'Dawn','1');

2) in the query data from the database, to verify:
from the library, you can view the database you just created:
Here Insert Picture Description
in the database, the user query data in the table:
Here Insert Picture Description

Published 81 original articles · won praise 5 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_36205206/article/details/103819435