mysql常用命令
Mysql 命令
1. 登录数据库
mysql –h hostname –u username –p
enter password:******
2. 启动、停止数据库服务
net start mysql
net stop mysql
3. 创建用户
GRANT priv_type [(column_list)] [, priv_type [(column_list)] ...]
ON {tbl_name | * | *.* | db_name.*}
TO user_name [IDENTIFIED BY 'password']
[, user_name [IDENTIFIED BY 'password'] ...]
[WITH GRANT OPTION]
Grant 权限列表 on 数据库名.表名 to 用户名@主机名 identified by 口令
实例(1):授权给用户user1 所有数据库的所有权限,并且可以在任何主机连接数据库grant all privileges on *.* to user1@”localhost” identified by ‘123456’
Grant all privileges on *.* to user1@”%” identified by ‘
实例(2):增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作(localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据库,只能通过MYSQL主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
4. 修改密码
修改密码有两种方法
1) 直接修改数据库服务器中mysql数据库的表 user
如:use mysql
Update user set password=password(“admin”) where user=’root’ and host=’localhost’;
Flush privileges;
注:flush 是清除缓存语句,
Flush PRIVILEGES 从mysql数据库授权表中重新装载权限。
2) 使用set password语句
Set password for root@localhost=password(‘
5. 创建数据库
1) 查看数据库
Show databases
2) 创建数据库
Drop database if exists studentinfo
Create database studentinfo
如果存在数据库studentinfo 则删除重新建立
6. 创建表
1) use studentinfo
drop table if exists student;
create table student(
id int not null auto_increment,
name varchar(20) not null default ‘name’,
math int not null default 60,
primary key (id));
)
Desc studentinfo 查看表结构
7. 插入操作
Insert into studentinfo values(1,’liying’,98);
Insert into studentinfo (id,name) values(1,’liying’);
可以从文件中导入数据
Load data infile ‘student.txt’ into table student
8. 查询、删除、修改操作
直接在 mysql>命令符下 运行sql语句即可
9. 修改表结构
1) 为表student增加一列english
Alter table student add (English int not null default 60);
2) 修改name的数据类型
Alter table student modify name varchar(30) not null;
3) 修改english 的默认值
Alter table student alter English set default 100
4) 删除english的默认值
Alter table student alter English drop default;