MySQL常用命令

一、连接命令

命令描述示例
-u用户名mysql -uroot
-p密码mysql -uroot -proot
-h连接地址mysql -uroot -proot -h127.0.01
-P端口号mysql -uroot -proot -h127.0.01 -P3306
-e免交互mysql -uroot -proot -h127.0.01 -P3306 -e “show databases;”
<导入数据库mysql -uroot -proot < db.sql

导入数据库也可以使用以下命令

rem 1、登录数据库
mysql -uroot -proot
rem 2、执行sql语句
source db.sql

两者区别在于:

  • mysql < 遇到报错会中止
  • source 遇到报错不会中止

二、管理命令

-- 创建用户
create user userName@'127.0.0.1' identified by 'userPassword';
-- 查询用户
select user,host,authentication_string from mysql.user;
-- 删除用户
drop user userName@'127.0.0.1';
-- 更改用户密码
alter user root@'localhost' identified by 'newPassword'; 

-- 创建数据库
create database db01;
-- 删除数据库
drop database db01;
-- 使用哪个库
use db01;
-- 查看表结构
desc table_name;

扩展:查看表结构命令

-- 查看表结构
desc table_name;
-- 查看表中列的注释信息 db:表所在数据库 table_name:查询的表
select * from information_schema.columns where table_schema = 'db'  and table_name = 'table_name' ; 
-- 查看列名和注释
select column_name, column_comment from information_schema.columns where table_schema ='db' and table_name = 'table_name' ;
-- 查看表的注释
select table_name,table_comment from information_schema.tables where table_schema = 'db' and table_name ='table_name'
-- 查看表生成的DDL
show create table table_name;

三、权限命令

-- with grant option 【慎用!】超级管理员拥有的,如果想要某个用户成为超级管理员,可在后面加上这个参数列如:
grant all on userName.* to userName@'127.0.0.1' identified by 'userPassword'; with grant option);

-- 查看权限
show grants for userName@'127.0.0.1';

-- 授权
grant ALL on *.* to userName@'127.0.0.1' identified by 'password';

-- 回收权限
revoke all on *.* from userName@'127.0.0.1';

四、初始化命令

mysqld --initialize-insecure --user=mysql --console

启动数据库到维护模式,更改用户密码命令
rem 启动数据库到维护模式
mysqld_safe --skip-grant-tables --skip-networking & 
rem 更改数据库密码,改完正常启动即可
alter user root@'localhost' identified by 'newPassword';
rem 新设置用户或更改密码后需用flush privileges刷新MySQL的系统权限相关表
flush privileges;

注意:更改完数据库密码后,维护模式的进程还在,–skip-networking 这个参数是禁止用户远程登录,需要将这个进程kill掉,否则无法进入数据库

五、show语句

命令描述
show databases查看所有数据库名
show tables查看当前库下的表名
show tables from world查看world数据库下的表名
show create database查看建库语句
show create table查看建表语句
show grants for root@‘localhost’查看用户权限信息
show charset查看所有的字符集
show collation查看校对规则
show full processlist查看数据库连接情况
show status查看数据库的整体状态
show status like ‘%lock%’模糊查看数据库的整体状态
show variables查看数据库所有变量情况
show variables like ‘%innodb%’模糊搜索数据库变量情况
show engines查看所有支持存储引擎
show engine innodb status查看所有innodb存储引擎状态情况
show binary logs查看二进制日志情况
show binlog events in查看二进制日志事件
show relaylog events in查看relay日志事件
show slave status查看从库状态
show master status查看数据库binlog位置信息
show index from查看表的索引情况
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值