MySQL 5.7的學習筆記

說明:本文爲Windows下MySQL 5.7的學習筆記
標籤:MySQL啓停、MySQL維護、MySQL備份恢復、修改MySQL參數、修改MySQL密碼、
溫馨提示:如果您發現本文哪裏寫的有問題或者有更好的寫法請留言或私信我進行修改優化


-- 註冊MySQL服務(一般不需要手動執行)
cd C:\Program Files\MySQL\MySQL Server 5.7\bin\
mysqld --install


-- 啓停(MySQL的服務名一般是帶版本號的)
cd C:\Program Files\MySQL\MySQL Server 5.7\bin\
net stop mysql57
net start mysql57
mysqladmin -u root -p shutdown


-- 登錄
cd C:\Program Files\MySQL\MySQL Server 5.7\bin\
mysql -u root -h localhost -P 3306 -proot
show databases;
status;

-- 查看狀態
show innodb status;
show global status;


-- 參數
✔ 自動提交
※ 參數:autocommit
※ 臨時關閉自動提交
show variables like 'autocommit';
set autocommit=0;            會話級別
set global autocommit=0;    全局級別
※ 永久關閉自動提交
修改my.ini,添加一行參數
重啓MySQL進行查驗
[mysqld]
autocommit=0
✔ UNDO最大未提交數
※ 參數:innodb_max_purge_lag
show variables like 'innodb_max_purge_lag';
set global innodb_max_purge_lag=1000;
✔ 排序緩存大小
※ 參數:sort_buffer_size
show variables like 'sort_buffer_size';
set global innodb_max_purge_lag=1000000;
✔ 最大登錄嘗試
※ 參數:max_connect_errors
show variables like 'max_connect_errors';
show variables like 'max_error_count';
set global max_connect_errors=4;
set global max_error_count=3;
use performance_schema;
select * from host_cache;
✔ 禁用MySQL-DNS
※ 參數:skip_name_resolve
show variables like 'skip_name_resolve';
✔ BIN日誌刷盤頻率
※ 參數:sync_binlog
show variables like 'sync_binlog';
✔ 查詢結果集緩存
※ 參數:query_cache
※ 說明:該參數有利有弊,適用於OLAP
show variables like '%query_cache%';
show global status like'%Qcache%';
set global query_cache_type=1;
set global query_cache_size=3M;
✔ 會話
※ 參數:connect
show variables like '%connect%';
✔ 輸出死鎖信息到錯誤日誌中
※ 參數:
show variables like 'innodb_print_all_deadlocks';
✔ 控制併發執行的線程數
※ 參數:innodb_thread_concurrency
show variables like 'innodb_thread_concurrency';
✔ 表打開緩存
※ 參數:table_open_cache
show variables like 'table_open_cache';
✔ 表定義緩存
※ 參數:table_definition_cache
show variables like 'table_definition_cache';
✔ xxx
※ 參數:
show variables like 'xxx';
✔ xxx
※ 參數:
show variables like 'xxx';
✔ xxx
※ 參數:
show variables like 'xxx';


-- 權限
show grants for 'zu'@'%' ;
grant select on zd.t to 'zu'@'%' ;
grant all privileges on zd to 'zu'@'%' ;
grant all privileges on *.* to 'zu'@'%' ;
grant all on *.* to 'zu'@'%' ;
flush privileges;


-- 用戶
drop user if exists 'zu'@'%';
create user if not exists 'zu'@'%' identified by 'zu';
set password for 'zu'@'%'=password('zu');
alter user 'root'@'localhost' identified by 'root';

-- 修改密碼
※ 知道舊密碼
mysqladmin -u root -p password new
※ 忘記舊密碼
vi C:\ProgramData\MySQL\MySQL Server 5.7\my.ini
臨時添加參數
[mysqld]
skip-grant-tables
重啓MySQL服務
無密碼登錄
mysql -u root -p
隨便輸個密碼
修改root密碼
-- <5.7
update mysql.user set password=password('aaa') where user='root';
-- ≥5.7
update mysql.user set authentication_string=password('aaa') where user='root';
select * from mysql.user where user='root'\G


-- 初始化
use sys;
drop database zd;
create database if not exists zd;
use zd;
create table t(id int(9) primary key,name char(20));
insert into t values(1,'A');
insert into t values(2,'B');
insert into t values(3,'C');
commit;
insert into t values(4,'D');
rollback;
select * from t;

create view v as select * from t;

create function f();


-- 臨時停止記錄bin-log
※ 參數:Set sql_bin_log=0
※ 說明:在會話(session)中運行


-- 刷新日誌
mysql>flush logs;
cmd  >mysqladmin flush-logs
cmd  >mysqladmin refresh

-- General日誌
※ 通用日誌(開啓)(通常臨時開啓一會)
show variables like '%gene%';
set global log_output='TABLE,FILE';
set global general_log=on;
※ 通用日誌(關閉)
set global log_output='FILE';
set global general_log=off;
show variables like '%gene%';

-- 慢日誌
show variables like '%slow%';

-- redo-log
show variables like '%innodb_log%';
【未完待續】【增刪改】


-- BIN-log
show variables like '%binlog%';
show variables like '%log_bin%';
※ 添加參數
[mysqld]
server_id=99
log_bin=mysql-bin
binlog_format=row
expire_logs_days=30
※ 重啓服務
net stop mysql57
net start mysql57
show variables like '%binlog%';
show variables like '%log_bin%';
show variables like '%expire%';

-- 獨立表空間
show variables like '%innodb_file_per_table%';
※ 關閉
set global innodb_file_per_table=0;
※ 開啓(默認)
set global innodb_file_per_table=1;

-- 獨立UNDO(安裝後無法修改)
show variables like '%undo%';
[mysqld]
innodb_undo_tablespaces=3;
set global innodb_undo_logs=128;  
set global innodb_undodirectory=C:\ProgramData\MySQL\MySQL Server 5.7\Data;

-- 臨時表空間
※ 查看臨時表空間參數(5.7版本開始使用)
show variables like '%innodb_temp_data_file_path%';
※ 查看臨時文件參數(<5.7版本適用)
show variables like '%tmp%';

-- 導入導出
※ 常規導出
mysqldump -uroot --hex-blob db_name > db_name.sql
※ 壓縮導出
mysqldump -uroot --hex-blob db_name |gzip > db_name.sql.gz
※ 導入數據
mysql < db_name.sql


-- 文件大小
※ 添加共享表空間數據文件
innodb_data_file_path = ibdata1:12M;ibdata2:20M:autoextend
說明:
    innodb_data_file_path的值應該爲一個或多個數據文件規格的列表。如果要命名一個以上的數據文件,請用分號“;”分隔它們。其語法格式爲:innodb_data_file_path=datafile_spec1[;datafile_spec2]...
    ibdata1:12M此處的大小必須是四捨五入後的值,不能捨入過大,如果把12M舍入爲15M則會導致MySQL啓動失敗
※ 修改事務日誌(需要重啓並MV移除舊日誌)
innodb_log_file_size = 50M


-- 統計信息
select * from information_schema.tables where table_name='T' \G


-- 查看會話列表
show processlist;


-- 監控工具
iotop        各進程的IO
smartctl    SSD監控
Nagios        表監控
swatch        日誌監控
cacti        MySQL監控


-- 複製
※ 判斷主從是否一致
工具:pt-table-checksum
※ 配置(從庫)
[mysqld]
log_slave_updates = 1
read_only = 1
slave_compressed_protocol=1
MySQL>
change master to 
master_host='1.1.1.99',
master_user='m',
master_password='m',
master_log_file='mysql-bin.000034',
master_log_pos=434;
MySQL> start slave;
MySQL> show slave status \G
※ 參數
show variables like '%max_allowed_packet%';        從庫接受大小
show variables like '%slave_exec_mode%';        從庫執行模式
show variables like '%slave_compressed_protocol%';        壓縮
show variables like '%slave_net_timeout%';        超時
※ 過濾(主庫)
binlog-do-db=db_name
binlog-ignore-db=db_name
※ 過濾(從庫)
--replicate-ignore-db=db_name
--replicate-ignore-table=db_name.tbl_name
--replicate-wild-ignore-table=foo%.bar%
※ 從庫跳過錯誤繼續應用
--slave-skip-errors
※ 禁止slave隨MySQL自啓
skip-slave-start
※ 從庫日誌刷盤方式
sync_master_info = 1
sync_relay_log = 1
sync_relay_log_info = 1
※ 延時複製
說明:該命令表示後臺運行這個工具10分鐘(默認是永久運行的),從庫保持一直滯後主庫1分鐘,間隔15秒每檢查一次,那麼理論上是延遲了1分鐘15秒。
pt-slave-delay u=xxxx,S=/tmp/mysql.sock,p=password --delay 1m --interval 15s --run-time 10m --log /path/to/delay.log –
daemonize

 

※ 如果您覺得文章寫的還不錯, 別忘了在文末給作者點個贊哦 ~

over

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章