101人参与 • 2026-08-10 • Powershell
mysql -u root -p -e "load data local infile '/path/to/file.csv' into table stu.students fields terminated by ',' lines terminated by '\n' ignore 1 rows;"
注意:需确保 mysql 启用了
local_infile(默认可能关闭):
show variables like 'local_infile'; -- 查看 set global local_infile = 'on'; -- 临时开启(需权限)
--local-infile=1:
mysql --local-infile=1 -u root -p
ignore 1 rows 跳过 csv 的标题行。mysqldump -u root -p123456 stu students > /root/data/mydb.sql
⚠️ 安全警告:密码明文写在命令行会泄露(可通过
history查看)。
推荐做法:
- 使用配置文件(
~/.my.cnf):然后:[client] user=root password=123456mysqldump stu students > /root/data/mydb.sql- 或交互式输入密码(不写
-p123456,只写-p)
mysqldump -u root -p --no-create-info stu students > data_only.sql
mysqldump -u root -p --no-data stu students > schema_only.sql
alias db_backup='mysqldump -u root -p --single-transaction --quick --lock-tables=false stu students | gzip > /root/data/mydb_$(date +%y%m%d).sql.gz'
参数说明:
--single-transaction:innodb 一致性快照(避免锁表)--quick:逐行读取,防内存溢出--lock-tables=false:配合--single-transaction避免锁
-- 查看建表语句(修正拼写错误:show create table) show create table students\g -- 查看当前数据库 select database(); -- 或 status; -- 显示更多信息,包括当前库
-- 前10条 select * from students limit 10; -- 分页:跳过前20条,取10条(第3页) select * from students limit 20, 10; -- 或 select * from students limit 10 offset 20;
注意:
limit无order by时结果顺序不确定!
-- 推荐指定位置(如 first / after) alter table students add column enroll_date date default '2025-12-12' after name;
| 操作 | 语法 | 用途 |
|---|---|---|
| modify | modify column_name new_type [options] | 改类型/属性,不改名 |
| change | change old_name new_name new_type [options] | 可改名+改类型 |
-- modify:只改类型和注释 alter table students modify datet datetime comment '入学时间'; -- change:改名 + 改类型 alter table students change stu_id student_id varchar(50) not null;
🔔 重要:
modify必须重写完整字段定义(类型、是否为空、默认值等),否则会被重置!
alter table students drop column length;
-- 修改表字符集(不影响已有数据编码!) alter table students convert to character set utf8mb4 collate utf8mb4_unicode_ci; -- 更彻底(同时转数据): alter table students convert to character set utf8mb4;
建议使用
utf8mb4(支持 emoji),而非utf8(mysql 的伪 utf8)
rename table students to student_info; -- 或 alter table students rename to student_info;
-- 删除表(结构+数据全丢) drop table students; -- 安全写法:先检查是否存在 drop table if exists students;
#!/bin/bash
date=$(date +%y%m%d_%h%m)
backup_dir="/backup/mysql"
db="stu"
table="students"
user="root"
mkdir -p $backup_dir
mysqldump --single-transaction -u$user $db $table | gzip > $backup_dir/${db}_${table}_$date.sql.gz
echo "$(date): backup completed: ${db}_${table}_$date.sql.gz" >> /var/log/db_backup.log# 每天凌晨2点备份 0 2 * * * /root/scripts/db_backup.sh
| 项目 | 建议 |
|---|---|
| 密码 | 不要明文写命令行,用 .my.cnf 或交互输入 |
| 备份 | 用 --single-transaction + 压缩 + 保留多版本 |
| 字符集 | 统一用 utf8mb4 |
| alter | 大表结构变更谨慎操作(可能锁表) |
| load data | 注意文件权限、路径、分隔符匹配 |
| 权限 | 生产环境避免用 root,创建专用账号 |
到此这篇关于shell环境下mysql数据库常用操作指南的文章就介绍到这了,更多相关shell mysql常用操作内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论