MySql常用命令

第一、mysql服务的启动和停止

1
2
3
4
service mysqld start
service mysqld stop
systemctl start mysqld
systemctl stop mysqld

第二、登陆mysql

语法如下:

1
mysql -u用户名 -p用户密码

键入命令mysql -uroot -p, 回车后提示你输入密码,输入12345,然后回车即可进入到mysql中了,mysql的提示符是:

1
mysql>

注意,如果是连接到另外的机器上,则需要加入一个参数-h机器IP -P端口

第三、增加新用户和权限

格式:

1
grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"

如,增加一个用户user1密码为password1,让其可以在本机上登录, 并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:

1
2
3
4
5
---部分权限---
grant select,insert,update,delete on *.* to user1@localhost Identified by "password1";

---所有权限---
grant all privileges on *.* to user1@localhost Identified by "password1";

如果希望该用户能够在任何机器上登陆mysql,则将localhost改为”%”。
如果你不想user1有密码,可以用命令将密码去掉。

1
grant select,insert,update,delete on mydb.* to user1@localhost identified by "";

第四、操作数据库

登录到mysql中,然后在mysql的提示符下运行下列命令,每个命令以分号结束,常用命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
---显示数据库列表---
show databases;

---显示库中的数据表---
use mysql;
show tables;

---显示数据表的结构---
describe 表名;

---建库与删库---
create database 库名;
drop database 库名;

---建库与删库---
use 库名;
create table 表名(字段列表);
drop table 表名;

---清空表中记录---
delete from 表名;

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

---退出---
exit(回车)

版权所有,如有侵权请联系我