启动mysql服务
systemctl start mysqld 或 systemctl restart mysqld
systemctl status mysqld
systemctl enable mysqld
停止mysql服务
systemctl stop mysqld
查看mysql进程
ps -ef |grep mysql
登陆mysql
mysql -uroot -p
mysql -h 127.0.0.1 -u 用户名 -p
列出所有数据库
show databases;
进入数据库
use test_db;
列出数据库所有表
show tables;
查看表结构
desc table_name;
创建 test_db数据库
create database test_db;
删除test_db数据库
drop database test_db;
删除数据库表
drop table table_name;
删除表里面记录
delete from table_name;
查询数据库表数据
select * from 库.表;
查看host,user两字段的值
select Host,User from mysql.user;
select * from t1 where id=1 and age =’jfedu’
select * from t1 where rowkey like 'CZ|192.168.1.%' limit 16000
SELECT * from T_LIGHT_DATA where CITY='CZ';
SELECT * FROM T_USER_LIGHT WHERE ""UPDATE_TIME"" >='20210701*' LIMIT 10;
SELECT count(1) FROM T_USER_LIGHT a where a.CITY='CZ' AND a.NET_MODE='FTTH'
create table t1 (id varchar(20),name varchar(20)); 创建名为t1表,并创建两个字段,id、name,varchar表示设置数据长度,用字符来定义长度单位,其中1汉字=2字符=2Bytes;
insert into t1 values ("1","jfedu"); 向表中插入数据;
alter table t1 modify column name varchar(20); 修改name字段的长度;
update t1 set name='jfedu.net' where id=1; 修改name字段的内容;
flush privileges; 刷新权限;
show variables like '%char%'; 查看数据库字符集;
show engines; 查看MySQL存储引擎;
show variables like '%storage_engine%';查看MySQL默认的存储引擎;
alter table t1 engine=innodb; 修改MySQL t1表存储引擎;
更改mysql用户登陆密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '5XvwRTW&';
配置数据库远程登陆
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'IDENTIFIED BY '5XvwRTW&' WITH GRANT OPTION;
添加test用户查询、插入、更新、删除的权限:
grant select,insert,update,delete on *.* to test@""%"" identified by ""123456"";
grant select,insert,delete,update,create,drop on *.* to test@""%"" identified by ""123456"";
flush privileges;"
查看mysql工作进程
show processlist;
查看主从状态
show slave status;
mysql数据库备份与恢复
mysqldump -u root -p test_db > /test_db.sql
mysql -u root -p test_db.sql < /test_db.sql
mysql数据库数据导入
use test_db;
source /test_db.sql;
重置mysql root密码
mysqladmin -u root -p password “新密码” 回车后要求输入旧密码
Update mysql.user set password=password(‘新密码’) where user=’root’;
flush privileges;
grant all on *.* to root@’localhost’ identified by ‘新密码’;
设置mysql密码长度:
set global validate_password_policy=0;
set global validate_password_length=1;
grant all privileges on *.* to 'root'@'%' identified by '123456';
#vi /etc/my.cnf #忘记密码,恢复密码
--skip-grant-table
mysql -uroot -p'mysqlroot' -e "ALTER USER 'root'@localhost IDENTIFIED BY '密码'
mysql -uroot -p123456 data -e ""select bn as '编码', bonusSn as '券码', useStartDate as '开始时间', useEndDate as '结束时间','吉祥红52度两支装' as '商品', if(status=1,'已使用','未使用') as '是否使用', order_id as '订单号', member_id as '使用会员' from voucher where vouchertype_id=609
order by bn asc "" > info.txt
purge binary logs to 'mysql-bin.000354'; #清理mysql binlog日志SET GLOBAL expire_logs_days = 3; #设置binlog保留3天FLUSH LOGS;show variables like 'expire_logs_days'; |
mysql -u root -p> purge master logs to 'mysql-bin.010’; //清除mysql-bin.010日志> purge master logs before '2020-02-28 13:00:00'; //清除2020-02-28 13:00:00前的日志> purge master logs before date_sub(now(), interval 90 day); //清除90天前的bin日志 |
数据库备份 |
备份所有数据库 mysqldump -uroot -p --all-databases > all.sql |
备份数据库test mysqldump -uroot -p test > test.sql |
备份数据库test下的表emp mysqldump -uroot -p test emp > emp.sql |
备份数据库test下的表emp和dept mysqldump -uroot -p test emp dept > emp_dept.sql |
备份数据库test下的所有表为逗号分割的文本,备份到/tmp: mysqlddump -uroot -p -T /tmp test emp --fields-terminated-by ','shell> more emp.txt 1,z12,z23,z34,z4 |
# 备份全部数据库(包含存储过程、自定义函数及事件)mysqldump -uroot -p123456 --single-transaction -R -E --all-databases > /tmp/all_database.sql |
# 要求记录 binlog 位点信息 可用于搭建从库mysqldump -uroot -p123456 --single-transaction -R -E --all-databases --master-data=2 > /tmp/all_database.sql |
# 备份指定数据库mysqldump -uroot -p123456 --single-transaction -R -E --databases db1 > /tmp/db1.sqlmysqldump -uroot -p123456 --single-transaction -R -E --databases db1 db2 > /tmp/db1_db2.sql |
# 备份部分表mysqldump -uroot -p123456 --single-transaction db1 tb1 > /tmp/tb1.sqlmysqldump -uroot -p123456 --single-transaction db1 tb1 tb2 tb3 > /tmp/tb.sql |
# 导出某个表,数据以单行insert显示mysqldump -uroot -p123456 --single-transaction --skip-extended-insert db1 tb1 > /tmp/tb1.sql |
# 导出单表的部分数据mysqldump -uroot -p123456 --single-transaction db1 tb1 --where=" create_time >= '2021-06-01 00:00:00' " > /tmp/tb1.sqlmysqldump -uroot -p123456 --single-transaction db1 tb1 --where='id < 10' > /tmp/tb1.sql |
# 排除某些表导出mysqldump -uroot -p123456 --single-transaction --databases db1 --ignore-table=db1.tb1 --ignore-table=db1.tb2 > /tmp/db1.sql |
# 只导出结构或只导出数据mysqldump -uroot -p123456 db1 --no-data > /tmp/db1_jiegou.sqlmysqldump -uroot -p123456 db1 --no-create-info > /tmp/db1_data.sql |
# 只导出某个库的存储过程及自定义函数mysqldump -uroot -p123456 -d -t -R db1 > /tmp/db1_routine.sql |
# 远程导出 即MySQL服务端不在本地mysqldump -uroot -p123456 -h192.168.1.10 -P3306 --single-transaction --databases db1 > /tmp/db1.sql |
#只导出除了系统库外的所有库数据 mysql -uroot -p'123456' -e "show databases"|grep -Ev "Database|mysql|information_schema|performance_schema|sys"|xargs mysqldump -uroot -p --routines --single_transaction --master-data=2 --databases > mysql_dump.sql |
#导出所有数据库 mysqldump -uroot -p'123456' --routines --single_transaction --master-data=2 --all-databases > mysql_dump.sql |
mysql分区表过大,配置自动回收innodb_max_undo_log_size = 4Ginnodb_undo_log_truncate = ONinnodb_undo_tablespaces = 3 |
mysql> show global variables like 'table_open_cache';mysql> show global status like 'Open%tables';mysql> set global table_open_cache=6144;清空表缓存mysql>flush tables; 可以将open_tables 清零systemctl restart mysqld 可以讲opened_tables 清零 |
数据库恢复 |
mysql -uroot -p db_name < backfile |
使用 mysqlbinlog 恢复自 mysqldump 备份以来的 binglog ./mysqlbinlog --no-defaults /data/mysql/mysql-bin.000002 | ./mysql t2 |
基于时间点恢复 上午 10 点发生了误操作 mysqlbinlog --stop-date="2017-09-30 9:59:59" /data/mysql/mysql-bin.123456 | mysql -uroot -ppassword |
mysqlbinlog --start-date="2017-09-30 10:01:00" /data/mysql/mysql-bin.123456 | mysql -uroot -ppassword |
基于位置恢复 |
mysqlbinlog --start-date="2019-09-30 9:59:59" --stop-date="2019-09-30 10:01:00"/data/mysql/mysql-bin.123456 > /tmp/mysql_restore.sql |
mysqlbinlog --stop-position="368312" /data/mysql/mysql-bin.123456 | mysql -uroot -ppassword |
mysqlbinlog --start-position="368315" /data/mysql/mysql-bin.123456 | mysql -uroot -ppassword |
表的导出 select * from test into outfile '/data/mysql/outfile.txt' fields terminated by "," enclosed by '"'; |
如果您喜欢本文,就请动动您的发财手为本文点赞评论转发,让我们一起学习更多运维相关知识,最后请记得关注我。
,