博客
关于我
mysql5.7命令总结
阅读量:790 次
发布时间:2023-02-11

本文共 3102 字,大约阅读时间需要 10 分钟。

MySQL操作指南

1. 连接MySQL

1.1 连接本地MySQL

假设用户名和密码均为 root

mysql -u root -p

回车后输入密码 root 回车即可进入。

或直接使用:

mysql -uroot -proot

注意:用户名前空格可有可无,密码前必须没有空格。

1.2 连接远程MySQL

假设远程主机IP为 110.110.110.110,用户名和密码均为 root

mysql -h 110.110.110.110 -uroot -proot

1.3 退出MySQL

exit

2. 修改密码

2.1 修改root密码

alter user root@localhost identified by 'newroot'

或主机地址为空时:

alter user root identified by 'newroot'

2.2 查看主机地址

mysql -uroot -prootselect user, host from user;

host 列即为主机地址。


3. 增加新用户

3.1 创建用户

create user 'tom1'@'localhost' identified by 'tom1';

或省略引号:

create user tom1@localhost identified by 'tom1';

或指定主机地址:

create user tom1@'192.168.1.%' identified by 'tom1';

4. 数据库操作

4.1 创建数据库

mysql> create database testdb;

4.2 显示数据库

mysql> show databases;

4.3 删除数据库

mysql> drop database testdb;

或删除不存在的数据库:

mysql> drop database if exists testdb;

4.4 连接数据库

mysql> use testdb;

4.5 当前数据库

mysql> select database();

5. 数据表操作

5.1 创建表

create table class (    id int(4) not null primary key auto_increment comment '主键',    name varchar(20) not null comment '姓名',    sex int(4) not null default '0' comment '性别',    degree double(16,2) default null comment '分数');

5.2 查看表详情

mysql> show full fields from class;

5.3 删除表

mysql> drop table mytable;

或删除不存在的表:

mysql> drop table if exists mytable;

6. 表数据操作

6.1 插入数据

mysql> insert into class (name, sex, degree) values('charles', '1', '80.5');

6.2 插入多条记录

mysql> insert into class (name, sex, degree) values('charles', '1', '80.5'), ('tom', '1', '80.5');

6.3 查询数据

mysql> select * from class;

6.4 查询前几行

mysql> select * from class limit 2;

6.5 删除数据

mysql> delete from class where id = 9;

6.6 修改数据

mysql> update class set degree = '90.9' where id = 10;

7. 表结构管理

7.1 增加字段

mysql> alter table class add exam_type int(4) default null comment '考试类别' after sex;

7.2 修改字段

mysql> alter table class change name name_new varchar(50) not null;

7.3 删除字段

mysql> alter table class drop column remark;

8. 约束与索引

8.1 添加主键约束

mysql> alter table class add constraint pk_id primary key using btree (id);

8.2 添加唯一约束

mysql> alter table class add unique key uk_name using btree (name);

8.3 删除约束

mysql> alter table class drop key uk_name;

8.4 添加索引

mysql> alter table class add index name_index (name);

8.5 删除索引

mysql> alter table class drop index name_index;

9. 用户管理

9.1 修改密码

mysql> alter user root@localhost identified by 'newpassword';

9.2 添加用户

mysql> create user 'newuser'@'localhost' identified by 'newpassword';

9.3 删除用户

mysql> drop user 'newuser'@'localhost';

9.4 查询用户

mysql> select * from mysql.user;

10. 其他实用命令

10.1 查看表结构

describe class;

10.2 分析SQL语句

explain statement "SELECT * FROM class LIMIT 10";

10.3 查看系统变量

show global variables like 'max_connections';

10.4 查看状态信息

show table status \G;

10.5 查看触发器

show triggers;

10.6 查看数据库创建语句

show create table class\G;

11. 日期和时间操作

11.1 获取当前时间

select now();

11.2 日期格式化

select date_format(now(), '%Y-%m-%d %H:%i:%s');

11.3 时间戳转日期

select from_unixtime(unix_timestamp('2024-01-01 12:00:00'));

12. 数据库备份与恢复

12.1 使用mysqldump备份

mysqldump -u root -p --databases test > test.backup.sql

12.2 恢复数据库

mysql -u root -p < test.backup.sql

通过以上命令,您可以完成MySQL的基本操作。如果需要更详细的操作说明或高级功能,请参考MySQL官方文档或相关技术资料。

转载地址:http://rjbfk.baihongyu.com/

你可能感兴趣的文章
MySQL replace函数替换字符串语句的用法(mysql字符串替换)
查看>>
mysql replace用法
查看>>
Mysql Row_Format 参数讲解
查看>>
mysql select, from ,join ,on ,where groupby,having ,order by limit的执行顺序和书写顺序
查看>>
MySQL Server 5.5安装记录
查看>>
mysql server has gone away
查看>>
mysql skip-grant-tables_MySQL root用户忘记密码怎么办?修改密码方法:skip-grant-tables
查看>>
mysql slave 停了_slave 停止。求解决方法
查看>>
MySQL SQL 优化指南:主键、ORDER BY、GROUP BY 和 UPDATE 优化详解
查看>>
MYSQL sql语句针对数据记录时间范围查询的效率对比
查看>>
mysql sum 没返回,如果没有找到任何值,我如何在MySQL中获得SUM函数以返回'0'?
查看>>
mysql sysbench测试安装及命令
查看>>
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
MySQL Workbench 数据库建模详解:从设计到实践
查看>>