CREATE USER 'glkj'@'%' IDENTIFIED WITH mysql_native_password BY 'Rmodi_1130';
https://www.cnblogs.com/igoodful/p/15331716.html
克隆MYSQL搭建主從:
INSTALL PLUGIN clone SONAME 'mysql_clone.so';
或者
在配置文件中加入:
plugin-load-add=mysql_clone.so
這兩個方法只需要任意做一個就可以了热凹。
SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'clone';
本地克挛党俊:
clone local data directory ="/home/work/apple";
遠程克略ぱ帷:
在源庫創(chuàng)建克隆賬號并授權(quán):
CREATE USER clone IDENTIFIED by 'clone';
GRANT BACKUP_ADMIN ON *.* TO 'clone';
在目標庫中執(zhí)行:
set global clone_valid_donor_list ='192.168.137.5:3306';
clone instance from 'clone'@'192.168.137.5':3306 identified by 'clone';
克隆完后兩邊的庫就是一模一樣的了。
查看克隆進度:
# 在目標實例上執(zhí)行:
SELECT STATE, ERROR_NO, ERROR_MESSAGE FROM performance_schema.clone_status;
select
state,
cast(begin_time as datetime) as "start time",
case when end_time is null then lpad(sys.format_time(power(10,12) * (unix_timestamp(now()) - unix_timestamp(begin_time))), 10, ' ')
else lpad(sys.format_time(power(10,12) * (unix_timestamp(end_time) - unix_timestamp(begin_time))), 10, ' ')
end as duration,
source,destination,
binlog_file,binlog_position,
gtid_executed
from
performance_schema.clone_status \G;
在從庫中執(zhí)行:
change master to master_host='192.168.137.5',master_port=3306,master_user='repl',master_password='repl',master_auto_position=1;
start slave;
show slave status \G
mysql多實例管理:
mkdir -p /application/330{7,8,9}/data
cat >/application/3307/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/
socket=
log_error=
port=
server_id=7
log_bin=
EOF
cat >/application/3308/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/
socket=
log_error=
port=
server_id=8
log_bin=
EOF
cat >/application/3309/my.cnf <<EOF
[mysqld]
basedir=/application/mysql
datadir=/
socket=
log_error=
port=
server_id=9
log_bin=
EOF
mv /etc/my.cnf? /etc/my.cnf.bak
mysqld --initialize-insecure? --user=mysql? --datadir=/application/3307/data --basedir=/application/mysql
mysqld --initialize-insecure? --user=mysql? --datadir=/application/3308/data --basedir=/application/mysql
mysqld --initialize-insecure? --user=mysql? --datadir=/application/3309/data --basedir=/application/mysql
cd /etc/systemd/system
cp mysqld.service mysqld3307.service
cp mysqld.service mysqld3308.service
cp mysqld.service mysqld3309.service
MYSQL雙主雙從搭建:
MYSQL二進制的安裝方法:
1锄列、解壓源碼包
2涩咖、檢查系統(tǒng)中有沒有MariaDB晶通,如果有就要將其清除掉,否則就會有沖突
rpm -qa | grep mariadb
yum remove? mariadb-libs
3班眯、增加mysql用戶:
useradd mysql
4、編輯環(huán)境變量:
vi /etc/profile
export PATH=/application/mysql8/bin:$PATH
source? /etc/profile
5烁巫、更改目錄屬組:
chown -R mysql:mysql? /application/mysql8/
6署隘、初始化數(shù)據(jù)庫:
mysqld --initialize? --user=mysql? --lower-case-table-names=1? --basedir=/application/mysql8? --datadir=/application/mysql8/data
mysqld --initialize-insecure? --user=mysql --lower-case-table-names=1? --basedir=/application/mysql8 --datadir=/application/mysql8/data
7、創(chuàng)建配置文件:
cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql8
datadir=/application/mysql8/data
server_id=155
port=3306
[mysql]
EOF
8亚隙、拷貝文件:
cp? /application/mysql8/support-files/mysql.server? /etc/init.d/mysqld
cp? /home/data/mysql/support-files/mysql.server? /etc/init.d/mysqld
9磁餐、啟動mysql:
service mysqld restart
注意,讓復(fù)制用戶在每個服務(wù)器上登錄一下阿弃,看能不能登錄诊霹!這一點很關(guān)鍵!
change master to master_host='192.168.43.96',master_user='repl',master_password='repl',\
master_log_file=' binlog.000001',master_log_pos=155;
1恤浪、首先要創(chuàng)建一個和原表一樣的結(jié)果表畅哑,來存放要歸檔的數(shù)據(jù):
create table t2 like t1;
2、歸檔表水由,歸檔的表要有主鍵:
pt-archiver --source h=192.168.137.5,D=test,t=pt1,u=pt,p=toolkit \
--dest h=192.168.137.5,D=test,t=pt2,u=pt,p=toolkit --where 'id <4' --no-check-charset --no-delete --limit=1000 \
--commit-each --progress 1000? --statistics
建立一個用戶來做歸檔:
create user pt identified with mysql_native_password? by 'toolkit';
grant all on *.*? to? pt;
pt-archiver --source h=192.168.137.5,D=test,t=pt1,u=pt,p=toolkit \
--dest h=192.168.137.5,D=test,t=pt2,u=pt,p=toolkit --where 'id<4' --no-check-charset --no-delete --limit=100 \
--commit-each --progress 200 --statistics
歸檔完清理原表的數(shù)據(jù):
pt-archiver --source h=192.168.137.5,D=test,t=pt1,u=pt,p=toolkit --where 'id<4' --purge --limit=1 --no-check-charset
pt-archiver --source h=192.168.137.5,D=test,t=pt1,u=pt,p=toolkit \
--where 'id<4' --purge --limit=1 --no-check-charset
PT工具的使用荠呐,在線修改表結(jié)構(gòu):
pt-online-schema-change? --user=pt --password=toolkit --host=192.168.137.5 \
--alter "add column state int not null default 1" D=test,t=pt1? --print --execute
在線建立索引:
pt-online-schema-change? --user=pt --password=toolkit --host=192.168.137.5 \
--alter "add index idx(state)" D=test,t=pt1 --print --execute
檢查主從同步的表是否一致:
pt-table-checksum --nocheck-replication-filters? --no-check-binlog-format? \
--replicate=pt.checksums \
--create-replicate-table --databases=test --tables=pt1 \
h=192.168.137.5,u=checksum,p=checksum,P=3306
pt-table-sync? --replicate=pt.checksums? --databases test --tables pt1 \
h=192.168.137.5,u=checksum,p=checksum,P=3306 h=192.168.137.6,u=checksum,p=checksum,P=3306 \
pt-table-sync? --replicate=pt.checksums? --databases test --tables pt1 \
h=192.168.137.5,u=checksum,p=checksum,P=3306 h=192.168.137.6,u=checksum,p=checksum,P=3306 \
--execute
select substring(md5(rand()),1,5);
壓力測試:
mysqlslap? --defaults-file=/etc/my.cnf --concurrency=100 --iterations=1 \
--create-schema='test' --query="select? *? from? test.t_1 where name='jin'"? \
engine=innodb --number-of-queries=2000? \
-uroot -poracle? -verbose
mysqlslap -uroot -poracle --concurrency=100 --iterations=1 --auto-generate-sql \
--auto-generate-sql-load-type=mixed --auto-generate-sql-add-autoincrement \
--engine=innodb? --number-of-queries=5000
htop? ,iotop ,
iftop? etho? ---- 查看網(wǎng)卡的流量
配置延遲從庫:
stop slave;
change master to master_delay=3600;
start slave;
show slave status \G
延遲故障恢復(fù):
stop salve sql_thread;
show relaylog events in 'bin.000002';
mysqlbinlog --start-position=626 --stop-position=1299? db01-relay-bin.00002 >/tmp/relay.sql
備份全庫:
mysqldump -uroot -poracle -A -R? --triggers? --set-gtid-purged=OFF? >full.sql? ?
在從庫中進行恢復(fù):
mysql -uroot -p? <full.sql? ? ? ?
快速恢復(fù)測試環(huán)境:
主庫:
reset master;
從庫:
drop database delay;
stop slave;
reset salve all;
在從庫中執(zhí)行:
stop slave;
change master to
master_host='192.168.137.5',
master_user='repl',
master_password='repl',
master_port=3306,
master_log_file='binlog.000001',
master_log_pos=3609,
master_connect_retry=10;
change master? to
master_host='192.168.137.5',
master_user='repl',
master_password='repl',
master_auto_position=1;
start slave;
過濾復(fù)制的配置:在從庫的配置文件中加入一行即可:
replicate_do_db=test
show collation;? 查看所有支持的校對規(guī)則
show master status; 查看mysql當(dāng)前使用的二進制日志信息
show? relaylog events in '' ; 查看中繼日志的事件信息
翻譯binlog日志:
mysqlbinlog --base64-output=decode-rows? -vvv? mysql-bin.00004
截取二進制日志:
mysqlbinlog --start-position=219? --stop-position=335? mysql-bin.000004 >/tmp/a.sql
mysqlbinlog -d? test? mysql-bin.000004? 只顯示test庫的日志
全局事務(wù)ID:每個一個事務(wù)都會分配一個ID。GTID由SERVER_UUID+事務(wù)ID組成砂客。
開啟GTID模式只需要在配置文件中加入兩行就可以了:
enforce_gtid_consistency=on
gtid_mode=on
截取GTID:
show binlog events in 'binlog.000002';? 查看二進制日志的事件信息
截取BINLOG:這樣截取后泥张,在恢復(fù)的時候會報錯!
mysqlbinlog? --include-gtids='79de5cd0-78d0-11ec-b2ba-080027199538:1-3' binlog.000002>/tmp/gtid.sql
加上下面的參數(shù)就可以了:
mysqlbinlog? --skip-gtids? --include-gtids='79de5cd0-78d0-11ec-b2ba-080027199538:1-3' binlog.000002>/tmp/gtid.sql
--skip-gtids:在導(dǎo)出時鞠值,忽略原有的GTID信息媚创,恢復(fù)時生成最新的GTID信息
set sql_log_bin=0;? 臨時關(guān)閉恢復(fù)時產(chǎn)生的新二進制日志
source /tmp/gtid.sql
set sql_log_bin=1;? 開啟
開啟慢日志:
slow_query_log=1
slow_query_log_file=/var/log/slow.log
long_query_time=3
log_queries_not_using_indexes
分析慢日志,找到慢SQL:
mysqldumpslow -s -c -t 10? /var/log/slow.log
-c 表示次數(shù)
pt-query-diagest? ? /var/log/slow.log
alter table city add index idx_1(district(5))? 前綴索引
select district, group_concat(name)? from city? group by district;
LIMIT M,N? :跳過M行,顯示N行
LIMIT Y OFFSET X :跳過X行彤恶,顯示一共Y行
全備:
mysqldump -uroot -poracle -A >/backup/full.sql
備份多個庫:
mysqldump -uroot -poracle -B test db1 db2? >/backup/DB.sql
備份一個庫中的多個表:
mysqldump -uroot -poracle? test t_1 t_2 >/backup/T.sql
-R 存儲過程和函數(shù)
-E 事件
--triggers
--master-data=2
1.記錄備份時刻的binlog信息
2.自動鎖表
不加 --single-transaction ,溫備份
加了 --single-transaction? 對于innodb表不鎖表備份
--single-transaction
對于innodb表不鎖表钞钙,一致性的備份
全備:
mysqldump -uroot -poracle -A --master_data=2 --single-transaction -R -E --triggers >/tmp/full.sql
全備:
mysqldump -uroot -poracle -A -R --max_allowed_packet=128M --triggers? --set-gtid-purged=OFF \
--master-data=2 --single-transaction|gzip >/backup/full_$(date +%F).sql.gzip
升級完后執(zhí)行這個命令:
mysql_upgrade? -uroot -p123? -S /data/3308/mysql.sock
全備:
innobackupex --user=root --password=oracle --no-timestamp? /backup/full
第一次增量備份:
innobackupex --user=root --password=123 --no-timestamp? --incremental
--incremental-basedir=/backup/full? /backup/inc1
第二次增量備份:
innobackupex --user=root --password=123 --no-timestamp? --incremental
--incremental-basedir=/backup/inc1? /backup/inc2
恢復(fù):
innobackupex? --apply-log? --redo-only? /backup/full
innobackupex? --apply-log? --redo-only? --incremental-dir=/backup/inc1? /backup/full
innobackupex? --apply-log? --incremental-dir=/backup/inc2? /backup/full
innobackupex? --apply-log? /backup/full
判斷連接數(shù)是否夠用:
show variables like 'max_connections';
show status like 'Max_used_connections';
查看臨時表的緩沖區(qū)是否夠用:KEY_BUFFER_SIZE
show status like '%Created_tmp%';
雙一標準:
innodb_flush_log_at_trx_commit=1
sync_binlog=1
innodb_flush_method=0_direct
監(jiān)控鎖:
show status like 'innodb_row_lock%';
看被阻塞的事務(wù):
select? *? from? information_schema.innodb_trx where trx_state='LOCK WAIT';
查看鎖源鳄橘,誰鎖的我
select *? from? sys.innodb_lock_waits;
根據(jù)鎖源的PID,找到鎖源SQL的線程ID
select *? from performance_schema.threads where processlist_id=;
根據(jù)鎖源SQL線程ID芒炼,找到鎖源的SQL語句
select * from? performance_schema.events_statements_current where thread_id=;
一個表就是一個段瘫怜,一個段由多個區(qū)組成,一個區(qū)有64個連續(xù)的頁組成本刽,一個頁16K鲸湃,一個區(qū)就是1M