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

原创文章 53 获赞 52 访问量 5010

猜你喜欢

转载自blog.csdn.net/zzt_2009/article/details/105888317
今日推荐