mysql常用操作
有一个图形管理mysql的工具叫phpmyadmin,而如何在命令行下面来管理和操作mysql。

查看某个库的表
1 2
| mysql> use mysql; mysql> show tables;
|

查看表的字段
1
| mysql> desc pre_ucenter_vars;
|

查看建表的语句
1
| mysql> show create table pre_ucenter_vars\G;
|

当前是哪个用户

查看当前库
1
| mysql> select database();
|

创建库
1
| mysql> create database yanyi;
|

创建表
1
| mysql> create table tb1 (`id` int(4), `name` char(40)) ENGINE=MyISAM DEFAULT CHARSET=gbk;
|
tb1 是表名;第一个字段是 id
,格式是 int ,长度 4位;第二个字段 name
格式是 char 长度 40;指定 ENGINE 为 MyISAM ;字符集 DEFAULT CHARSET 为 gbk。

创建完成后查看



插入数据
1
| mysql> insert into tb1 values (1,'yanyi');
|

还可以继续插入

也可以只插入一个字段
1
| mysql> insert into tb1 (`id`) values (2);
|

1
| mysql> insert into tb1 (`name`) values ('docker');
|

1
| mysql> insert into tb1 (`name`,`id`) values ('redis',6);
|

查看数据库版本
1
| mysql> select version();
|

查看 mysql 状态

也可以用 like 过滤,% 通配
1
| mysql> show status like '%running';
|

1
| mysql> show status like '%buffer%';
|

修改 mysql 参数

1 2
| mysql> set global max_connections=200; mysql> show variables like 'max_connections';
|

如果记不住也可以用 % 通配
1
| mysql> show variables like 'max_connec%';
|

不过这些方法,重启就会变回以前配置,要永久生效需更改 /etc/my.cnf
查看 mysql 队列
1
| mysql> show processlist;
|

查看 mysql 变量
创建普通用户并授权
1 2 3 4 5
| mysql> grant all on *.* to user1 identified by '123456'; mysql> grant all on discuz.* to 'user2'@'192.168.0.%' identified by '123456'; mysql> grant all on discuz.* to 'user3'@'%' identifined by '123456';
|
all 代表所有权限;.代表所有库里的所有表,discuz.*代表 discuz 库里的所有表;to 后边是用户名;@ 后边是客户端 ip ,192.168.0.%代表整个网段,% 代表通配,直接用 % 代替 ip 就表示所有网段;identfied by 后边是密码。
用户创建完以后还没有即时生效,需要刷新下权限命令如下
1
| mysql> flush privileges;
|
更改密码
1
| mysql> update mysql.user set password=password("newpwd") where user='username';
|
查询
1
| mysql> select count(*) from mysql.user;
|

1
| mysql> select * from mysql.db;select * from mysql.db where host like '10.0.%'\G;
|

插入
1
| mysql> update tb1 set id=5 where name = 'docker';
|

清空表
1
| mysql> truncate table yanyi.tb1;
|
yanyi是库名,tb1是表名,清空以后表还在。

删除表
1
| mysql> delete from tb1 where name='redis'
|
这是删除表中指定的行

这是删除整个表

删除数据库
1
| mysql> drop database yanyi;
|

修复表
1
| mysql> repair table discuz.pre_forum_post;
|

以上就是一些常用的 mysql相关操作。还有个知识点,在 shell 的命令下去执行 mysql 的操作
-e 前面的 mysql 指的是库的名字, -e 选项后面双引号括起来的就是 mysql 的命令。

授权超级用户
1
| grant all privileges on *.* to 'tangnanbing'@'%' identified by '1qaz@WSX' with grant option;
|