mysql常用命令总结

来源:互联网 发布:凹半圆铣刀r4数据 编辑:程序博客网 时间:2024/06/18 06:01
原文地址:mysql常用命令总结作者:改变自己

   作为一名合格的Linux运维mysql数据库是必须掌握的技能,原来自己也断断续续的学习过mysql,只是在比较大的互联网公司都有专业的DBA,需要运维做的事情很少,所以掌握的东西还是不多,下面是在运维里比较常见的一些操作命令,原来工作里多多少少都用到过,不过每次可能也就用到几个命令,所以一直没有做个总结,隔一段时间就忘的差不多了,等到要用的时候又只能去百度谷歌了,很浪费时间,今天有点时间,归纳一下做个备忘吧~~

   以下的命令都经过测试,暂还没有写完,后续如果接触到其他有用的命令也会更新到这。

 

1、启停mysql

系统默认mysql启停:

[root@localhost ~]#/etc/init.d/mysqld start

[root@localhost ~]#/etc/init.d/mysqld stop

 

编译安装mysql启停:

[root@localhost ~]#/usr/local/mysql/bin/mysqld_safe --user=mysql&

[root@localhost ~]#/usr/local/mysql/bin/mysqladmin shutdown

 

如果编译安装后将源码包下的开机启动文件拷贝到了/etc/init.d下,也可以使用以上的启动方式。

[root@localhostmysql-5.5.23]cp support-files/mysql.server/etc/init.d/mysqld

 

mysqld启动脚本还有其他的功能参数:

Usage: /etc/init.d/mysqld{start|stop|status|condrestart|restart}

 

注:以下所有的操作默认是系统本机mysql

2、密码设置

设置初始密码:

[root@localhost ~]#mysqladmin -uroot password '111111'

 

清除密码:

[root@localhost ~]#mysqladmin -uroot password '' #密码为空

 

重置密码:

[root@localhost ~]#mysqladmin -uroot -p111111 password '222222'

 

登陆到mysql后修改密码:

[root@localhost ~]# mysql-uroot -p222222  #登陆

mysql> updatemysql.user set password=password('333333') whereuser='root';

mysql> flushprivileges;  #刷新权限

 

忘记密码后重置:

1)停止mysql

[root@localhost ~]#/etc/init.d/mysqld stop

kill进程

[root@localhost ~]# kill -9`ps -ef|grep mysql|grep -v grep|awk '{print $2}'`

2)使用--skip-grant参数启动mysql

[root@localhost ~]#mysqld_safe --skip-grant &

3)登陆mysql修改密码

[root@localhost ~]#mysql

mysql> updatemysql.user set password=password('111111') whereuser='root';

mysql> flushprivileges;

4)重启mysql

[root@localhost ~]#/etc/init.d/mysqld restart

 

3、连接mysql

本地连接:

[root@localhost ~]# mysql -uroot -p111111

 

远程连接:

[root@localhost ~]# mysql-h192.168.128.10 -uroot -p111111     #-h参数后跟IP

本机也可以加-h参数:

[root@localhost ~]# mysql-h127.0.0.1 -uroot -p111111

 

4、用户管理

查看用户权限:

mysql> showgrants for root@localhost;

 

授权:

mysql权限分全局级别、数据库级别、表级别、列级别,根据需要指定相应级别。

赋予root超级用户权利权限:

mysql> grantall privileges on *.* to root@'%' identified by'111111';

 

创建test全局权限并有授予别人的权限:

mysql> grantall privileges on *.* to test@'%' identified by '222222' with grantoption;

 

创建test用户并赋予cacti库所有权限:

mysql> grantall privileges on cacti.* to test@'%' identified by'222222';

 

创建test用户并赋予cacti库某些权限:

mysql> grantselect,delete,update,create,drop on cacti.* to test@'%' identifiedby '222222';

 

创建test用户并赋予cactihost表权限:

mysql> grantall privileges on cacti.host to test@'%' identified by'222222';

 

撤销用户权限:

mysql> revokeall on *.* from test;

 

REVOKE语句只是取消用户的权限,没有彻底删除用户,如果需要,可以用delete语句删除用户。

mysql> deletefrom mysql.user where user='test';

mysql> flushprivileges;

 

多条件匹配删除:

mysql> deletefrom mysql.user where user='test' and host='localhost';

 

5、备份恢复

mysqldump备份库:

[root@localhost ~]#mysqldump -u root -p111111 mysql>backup.sql

建议备份时加上参数:--lock-tables、--lock-all-tables、--single-transaction

--lock-all-tables, -x:

在开始导出之前,提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭--single-transaction 和 --lock-tables 选项。

--lock-tables:

它和 --lock-all-tables 类似,不过是锁定当前导出的数据表,而不是一下子锁定全部库下的表。本选项只适用于MyISAM 表,如果是 Innodb 表可以用 --single-transaction 选项。

--single-transaction:

该选项在导出数据之前提交一个 BEGIN SQL语句,BEGIN不会阻塞任何应用程序且能保证导出时数据库的一致性状态。它只适用于事务表,例如 InnoDB 和 BDB。

本选项和 --lock-tables 选项是互斥的,因为 LOCK TABLES 会使任何挂起的事务隐含提交。

要想导出大表的话,应结合使用 --quick 选项。

 

备份多个库:

[root@localhost ~]#mysqldump -u root -p111111 mysql test>backup.sql

 

备份所有库:

[root@localhost ~]#mysqldump -u root -p111111 --all-databases>all.sql

 

备份单库结构:

[root@localhost ~]#mysqldump -u root -p111111 -d mysql>backup.sql

 

压缩备份库:

[root@localhost ~]#mysqldump -u root -p111111 mysql | gzip>backup.sql.gz

 

备份表:

[root@localhost ~]#mysqldump -u root -p111111 mysql user>backup.sql

 

备份表结构:

[root@localhost ~]#mysqldump -u root -p111111 -d mysql user>user.sql

 

还原数据库:

[root@localhost ~]# mysql -uroot -p111111 <backup.sql

 

还原压缩的数据库:

[root@localhost ~]# gunzip<backup.sql.gz |mysql -uroot -p111111mysql

 

将本机mysql库拷贝至其他服务器的newdata库中:

[root@localhost ~]#mysqldump -uroot -p111111 mysql|mysql -h192.168.128.10 -uroot-p222222 -C newdata

 

mysqldump创建数据库的命令:

[root@Master ~]# mysqladmincreate newdata


2013.3.29 添加:

6、计数、查询、排序

mysql> selectcount(*) from mysql.user;   #统计表总行数

mysql> select user,host,password from mysql.userorder by host; #按某列顺序

mysql> select user,host,password from mysql.userorder by host desc;   #按某列逆序

mysql> select user,host,password from mysql.userorder by host limit 10;  #顺序排,取前10行

mysql> select user,host,password,count(*) frommysql.user group by host;  #去重、计数

+------+-----------+----------+----------+

| user | host     | password | count(*) |

+------+-----------+----------+----------+

| root | 127.0.0.1 |              1 | 

| root | localhost |              2 | 

+------+-----------+----------+----------+


7、安全删除binlog日志

mysql> show binary logs;

+------------------+-----------+

| Log_name        | File_size |

+------------------+-----------+

| mysql-bin.000005 |     10000| 

+------------------+-----------+

| mysql-bin.000006 |       98 | 

+------------------+-----------+

1 row in set (0.00 sec)

mysql> purge binary logs to 'mysql-bin.000006'; #删除mysql-bin.000006之前的日志

Query OK, 0 rows affected (0.01 sec)


mysql> show binary logs;

+------------------+-----------+

| Log_name        | File_size |

+------------------+-----------+

| mysql-bin.000006 |       98 | 

+------------------+-----------+


8、mysql查看表结构:

desc 表名;

show columns from 表名;

describe 表名;

show create table 表名;


use information_schema

select * from columns where table_name='表名';


9、查询

按某列值条件查询:

mysql> select * from userinfo wherename='cowboy';

取某列最大值:

select max(level) as level from userinfo;

+-------+

| level |

+-------+

|    14 |

+-------+

取某列最大值并输出整行:

mysql> select * from userinfo order by level desclimit 1;

+-------+--------+-------+-----------+------------+------+

| id    | name  | level | vip_level | experience | icon |

+-------+--------+-------+-----------+------------+------+

| 10003 | lisi   |   14 |        0 |      5572 |   0 |

+-------+--------+-------+-----------+------------+------+

更新上面查询行中的某列值:

mysql> update userinfo set name='zhangsan' orderby id level limit 1;

按某列从大到小排序,显示前10行:

mysql> select * from userinfo order by level desclimit 10;

另一种查询如果最大值有多行则都显示,如果只有一条数据则个上面一样:

mysql> select * from userinfo where level=(selectmax(level) from userinfo);


10、删除数据操作

mysql> delete from tables;


0 0