一、连接命令
命令 | 描述 | 示例 |
---|---|---|
-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 | 查看表的索引情况 |