---
title: "MySql:常用命令"
author: "Hobo86"
date: "2013-06-17T15:37:41Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2013-08-25T15:18:31Z"
id: 1839906
url: "https://hobo86.iteye.com/blog/1839906"
---

登入

mysql -h192.168.1.110  -uroot -ppassword

 

登出

quit/exit

 

查看数据库

show databases;

 

用户权限

添加

grant select on db.table to 'user'@'host';

grant select,update on \*.* to 'test'@'%';

撤销

revoke all on \*.* from 'test'@'%';

查看

show grants;

show grants for user@localhost

 

删除用户

delete from mysql.user where user='' and host='';

 

设置密码

update mysql.user set password=PASSWORD('123456') where user='root';

 

配置远程连接

GRANT ALL PRIVILEGES ON \*.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION; 

 

导出

用mysqldump命令行    
命令格式    
mysqldump -u　用户名 -p 数据库名 &gt; 数据库名.sql    
范例：    
mysqldump -u　root -p dbname &gt; filepath.sql  

```sql
//To export tofile(data only)
mysqldump -u [user]-p[pass]--no-create-db --no-create-info mydb > mydb.sql
//To export tofile(structure only)
mysqldump -u [user]-p[pass]--no-data mydb > mydb.sql
//To import todatabase
mysql -u [user]-p[pass] mydb < mydb.sql
```

 

 

导入

mysql&gt; use dbname             #切到要导入的数据库  
mysql&gt; source filepath.sql

 

变量查看/修改

show variables like '%slow%';

set global slow\_query\_log= 'ON';

 

查看状态

如show status like 'Qca%';

 

查看SQL中select条数

show status like 'Com\_sel%'; 显示的是一次会话的值！

show global status like "Com\_select";

 

 找回密码？

1、kill -TERM mysqld

2、conf中加入skip-grant-tables

3、service mysqld restart

4、mysql -uroot -p

5、修改密码

6、改回原配置并重启

 

Binlog：[http://dev.mysql.com/doc/refman/5.7/en/mysqlbinlog.html](http://dev.mysql.com/doc/refman/5.7/en/mysqlbinlog.html)

删除mysql-bin日志

[http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html](http://dev.mysql.com/doc/refman/5.7/en/purge-binary-logs.html)

PURGE BINARY LOGS TO 'mysql-bin.010';

PURGE BINARY LOGS BEFORE '2008-04-02 22:46:26';

 

清空表并使自增归0

TRUNCATE TABLE tablename

 

Mac无法登陆

Can't connect to MySQL server on '127.0.0.1' (61)

StevenMacBookAir:~ Hobo$ sudo su -

StevenMacBookAir:~ root# nohup /usr/local/mysql/bin/mysqld\_safe &

\[1] 464

StevenMacBookAir:~ root# appending output to nohup.out

exit

logout

StevenMacBookAir:~ Hobo$