[转+亲测]mysql常用命令

系统为CentOS6.4,mysql版本为5.1.67。

1、mysql服务的启动和停止
service mysqld start
service mysqld stop

2、登陆mysql
语法如下: mysql -u用户名 -p用户密码
键入命令mysql -u root -p, 回车后提示你输入密码,例如输入123456,然后回车即可进入到mysql中了。
mysql的提示符是:mysql>

3、显示数据库列表
show databases;

缺省有两个数据库:mysql和test。mysql库存放着mysql的系统和用户权限信息,我们改密码和新增用户,实际上就是对这个库进行操作。

4、显示库中的数据表
use mysql; //选中一个数据库
show tables;

5、显示数据表的结构
describe 表名;

显示该数据表由哪几个字段组成,数据类型分别是什么。

6、显示表中的记录
select * from 表名;

7、赋予用户权限同时添加新用户
格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”
例如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:
grant select,insert,update,delete on *.* to user1@localhost identified by “password1”;
赋予全部权限
grant all on *.* to user1@localhost identified by “password1″;

如果希望该用户能够在任何机器上登陆mysql,则将localhost改为”%”。

8、重新加载授权表
flush privileges;

9、删除数据库中用户
use mysql;
delete from user where user=”user1″ and host=”localhost”;

10、建库与删库
create database 库名;
drop database 库名;

11、建表与删表
use 库名;
create table <表名> (<字段名1> <类型1> [,..<字段名n> <类型n>]);
drop table 表名;

例如:
create table class(id int(4) not null primary key, name char(20) not null,degree double(16,2));
建表时要设计字段数据类型,是否为主键等多种属性。

12、往表中加入记录
insert into class values (“0001″,”zhang”,”99.8″);

13、更新表中数据
update class set degree=”100.1″ where id=”0001″;

14、清空表中记录
delete from 表名;

15、退出mysql
exit;