mysql常用命令总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql常用命令总结相关的知识,希望对你有一定的参考价值。
1.登录数据库
[[email protected] data]# mysql -uroot -p
2.更改已知用户的密码
[[email protected] ~]# mysqladmin -uroot -p123456 password "oldboy"
3.初始化root的密码
[[email protected] ~]# mysqladmin -uroot password "123456"
4.查看数据库
mysql> show databases;
5.使用数据库
mysql> use mysql;
Database changed
mysql>
6.查看数据库的表
mysql> show tables;
7.查看数据表的结构
mysql> describe user;
mysql> show columns from test;
8.从表中查找字段(此处查询的字段不区分大小写)
mysql> select Host,User from user; #直接查表
mysql> select host,user from mysql.user; #从哪个库的哪一个表
9.删除库中的数据(use数据库中的 用户)
mysql> drop user "sanlang"@"10.0.0.8";
mysql> drop user ""@"10.0.0.8";
或者:
mysql>delete from mysql.user where user="sanlang" and host="110.0.0.8";
10.查看当前的用户
mysql> select user();
11.查看当前所在的数据库
mysql> select database();
12.处理完用户要执行的命令
mysql> flush privileges;
13.导出数据库
[[email protected] ]#mysqldump -uroot -poldboy123 wordexpress -B|gzip >bak.sql.gz
14.把数据库脚本导入
先把备份解压,再导入 gunzip 2016-06-19.sql.gz
[[email protected] ]#mysql -uroot -poldboy123</tmp/bak.sql
此处有个坑,备份的目录不存在,数据将不会备份,即使看起来是成功的,备份检查
15.对数据库授权
在数据库中授权
mysql> grant all on wordpress.* to [email protected]‘172.16.1.%‘ identified by ‘123456‘;
16.远程连接数据库
[[email protected] ~]# mysql -ubbs -p123456 -h 172.16.1.51
17.登录多实例数据库
mysql -uroot -poldboy123 -h 10.0.0.7 -P3306
18.多实例数初始化密码
[[email protected] scripts]# mysqladmin -uroot password oldboy789 -S /data/3308/mysql.sock
19.mysql命令总结
-p, --password[=name]
-S 指定socket
-P 指定端口
-h 指定远程的IP
20.查看帮助
help creat database;
21查看字符集校对规则
mysql> show character set;
22.创建GBK字符集的数据库oldboy,并查看建立数据库的完整语句
mysql> create database oldboy CHARACTER SET gbk COLLATE gbk_chinese_ci;
mysql> show create database oldboy;
23.查看版本
mysql> select version();
24模糊查询
mysql> show databases like ‘%my%‘;
25.增加system并提升Wie超级管理员,即和root等价的管理员用户,只是名字不同而已
mysql>grant all privileges on *.* to [email protected]‘localhost‘ identified by ‘oldboy123‘ with grant option;
这个system用户不需要提前增加,默认会给加上去
26多实例用户修改密码
[[email protected] 3306]# mysqladmin -usystem -p3306 password oldboy123 -S /data/3306/mysql.sock
27更新密码
mysql> UPDATE mysql.user SET password=PASSWORD("123456") WHERE user=‘root‘ and host=‘localhost‘;
更新后
mysql> flush privileges;
28.查看创建库的命令
mysql> show create database oldboy;
29.like语句
mysql> show databases like ‘oldboy%‘;
mysql> show databases like ‘%oldboy%‘;
30.删除数据库
mysql> drop database oldboy
31.不跳出mysql执行linux命令行的命令
mysql> system cd /root
mysql> system ls
anaconda-ks.cfg install.log install.log.syslog
mysql>
32.查看当前处于的数据库
mysql> select database();
33.查看当前系统时间
mysql> select now();
34.创建用户,并授权(默认没有权限)
第一种授权
mysql> create user [email protected]‘localhost‘ identified by ‘oldboy123‘;
mysql> grant all on oldboy.* to [email protected]‘localhost‘;
第二种授权
mysql> grant all on oldboyutf8.* to [email protected]‘localhost‘ identified by ‘oldboy123‘; #此处用户不存也可以
第三种授权(示例)
mysql> grant all on oldboyutf8.* to [email protected]‘10.0.0.%‘ identified by ‘oldboy123‘;
mysql> grant all on oldboyutf8.* to [email protected]‘10.0.0.0/255.255.255.0‘ identified by ‘oldboy123‘; #不能用24,否则不生效
mysql> create user [email protected]‘localhost‘ identified by ‘oldboy123‘;
mysql> grant all on oldboy.* to [email protected]‘10.0.0.%‘;
35.查看用户的权限
mysql> show grants for [email protected]‘localhost‘;
36.回收权限
mysql> REVOKE INSERT ON oldboy.* FROM ‘oldboy‘@‘localhost‘;
回收oldboy用户对oldboy表的insert权限
37.查看用户具体权限信息
mysql> select * from mysql.user \G;
39.查看表的创建
mysql> show create table student;
40.查看数据库表的结构
mysql> desc student;
41.查看表的列
mysql> show columns from student;
42.删除主键
mysql> alter table student drop primary key; 反思:自增的主键删不掉
mysql> alter table student add primary key(id);
42.远程连接
mysql -uwordpress -poldboy123 -h 172.16.1.51 -P3306
43.查看用户所拥有的具体权限
mysql> select * from mysql.db where user=‘wordpress‘\G
44.创建主键和索引
mysql> create table student(
-> id int(4) not null auto_increment,
-> name char(20) not null,
-> age tinyint(3) not null default ‘0‘,
-> primary key(id),
-> key index_name(name)
-> )
-> ;
Query OK, 0 rows affected (0.09 sec)
mysql>
45.查看索引的命名;
mysql> desc student
mysql> show index from student;
mysql> show index from student \G; 看的比较清楚
46.删除索引
查看索引的名字:
mysql> show index from student;
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| student | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| student | 1 | index_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
+---------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2 rows in set (0.00 sec)
mysql>
mysql> alter table student drop index index_name;
47.给表添加索引(生产场景晚上建立索引)
mysql> alter table student add index index_name(name);
mysql> create index index_age on student(age);
49.以一个表的一个字段的前n个字符创建索引
mysql> create index index_age on student(age(n));
mysql> alter table student add index index_age(age(1));
50.给表的多个字段创建联合索引(条件越多,符合条件越少)
mysql> alter table stuent add index index_name_age(name(3),age(2));
mysql> create index index_name_age on student(name,age);
mysql> create index index_name_age on student(name(3),age(2)); #字段的前几个字符创建联合索引
51.对用户进行去重(或者说对特定的一列进行去重)
mysql> select count(distinct user) from mysql.user;
52.创建唯一索引
mysql>create uniq index index_id_name on student(name);
53.插入数据的操作
mysql> insert into student(id,name,age) values(1,"xiaoming",10);
54.批量插入数据
mysql> insert into grade(name,age) values("xiaoli",90),("sanlang",93);
55.创建一个表test,指定其引擎为innodb 字符集为gbk
mysql> create table test( id int(4), name varchar(10) ) engine=innodb charset=gbk;
mysql> alter table test ENGINE=MYISAM; #修改表的引擎
56.更新表中的字段
mysql> update test set name="oldgirl" where id=1;
57.已知表的后边你插入字段
mysql> alter table test add age int(2) after name ;
58.删除字段
mysql> alter table test drop age ; #删除age字段
59.删除表中的所有数据
mysql> truncate test; #删除数据文件,物理删除,比较快
mysql> delete from test; #逻辑删除,sql语句删除比较慢
60.查询前两行数据
mysql> select * from test limit 2; #查询表的前两行
mysql> select * from test limit 1,2; #从第一个之后查询两个,不包括第一个
61.查询数据并排序
mysql> select * from test order by id desc;
62.修改字段的类型
mysql>alter table test modify age char(4) after name;
63.修改字段的名称
mysql>alter table test change age oldboyage char(4) after name; #直接更改字段名称以及字段类型
64.修改表名称
mysql>rename table oldtable to newtable
65.复制表的结构,创建数据库的时候
mysql>create table grade like sanlang ;
65.复制表的结构和数据在创建数据的时候
mysql>create table student select * from sanlang ;
66.非交互式操作mysql数据库服务器
单条命令
[[email protected]boy ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show databases;"
多条命令:
[[email protected] ~]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "use backup;show tables;"
67.查看mysql的配置的命令
mysql> show variables;
[[email protected] oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like \"server_id\" ;"
[[email protected] oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like ‘server_id‘ ;" #查看server_id
[[email protected] oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like ‘%log_bin%‘ ;" #bin_log日志
[[email protected] oldboy]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e "show variables like ‘%slow%‘ ;" #慢查询是否开启
68.不重启数据库,修改数据库的配置文件
以后不重启修改数据的配置文件,都这么干
[[email protected] ~]# grep key_buffer /data/3306/my.cnf
key_buffer_size = 16M
[[email protected] ~]# set global key_buffer_size = 1024*1024*32 #临时生效,重启mysql失效
vim /data/3306/my.cnf
key_buffer_size = 32M
[[email protected] ~]# sed -i ‘s#key_buffer_size = 16M#key_buffer_size = 32M#g’/data/3306/my.cnf
(没有的话加进去,手动加进去)
69.杀死数据sql语句的进程
kill ID
70.查看当前会how global status话的数据库状态信息
mysql> show session status;
71.查看整个数据库的运行状态信息,很重要,做分析并做好监控
mysql> show global status ; #可以查看数据库select,update,。。 多少次
mysql> show global status like "%select%"; #查看这些命令执行的次数,可以按天处理,今天减去昨天的就是今天的
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| Com_insert_select | 21 | #插入的次数
| Com_replace_select | 0 |
| Com_select | 26 | #查询的次数,不同的查询会增减,同一条命令不会增加
| Select_full_join | 0 |
| Select_full_range_join | 0 |
| Select_range | 0 |
| Select_range_check | 0 |
| Select_scan | 58 |
+------------------------+-------+
8 rows in set (0.00 sec)
mysql>
72.查看innodb引擎的性能状态
show engine innodb status
74.查看标的列
mysql> show COLUMNS from test;
75.修改主键
alter table student change id id int primary key auto_increment;
76.将序排序和升序排序
mysql> select * from test order by id desc; #将序排序
mysql> select * from test order by id asc; #升序排序
77.导出数据文件
mysql> select * from test order by id asc into outfile ‘/tmp/file.txt‘;
78.批量插入数据
mysql> insert into test(name,age) values("xiaoming",18),("lili",20)
79.创建唯一索引
mysql>create uniqe index index_ind_name on student(name)
反思:数据库的停止 不要用kill -9,否则起不来,后果很严重,创建数据库的时候最好不要有特殊字符
本文出自 “小小三郎的博客” 博客,请务必保留此出处http://wsxxsl.blog.51cto.com/9085838/1792032
以上是关于mysql常用命令总结的主要内容,如果未能解决你的问题,请参考以下文章