在cmd命令行中的操作,需要先登录MySQL
无密码:mysql -u root
有密码:mysql -u root -p (会提示输入数据库密码)
如果直接在mysql命令行中操作,则没有登录MySQL这一步。
MySQL常用命令:
一、操作数据库和数据表:
- 显示数据库:show databases;
- 创建数据库: create database 数据库名;
- 切换数据库:use 数据库名;
- 显示数据库中的表:show tables from 数据库名;
- 列出所有表:show tables;
- 显示数据表结构:describe 表名;
- 进入mmall数据库中,创建表:
mysql> use mmall Database changed mysql> create table table_01( -> id INT NOT NULL AUTO_INCREMENT, -> title VARCHAR(100) NOT NULL, -> PRIMARY KEY (id) -> ); Query OK, 0 rows affected (0.10 sec)
- 删除数据库:drop database 数据库名;
- 删除数据表:drop table 数据表名;
二、查看、删除、插入、刷新用户:
- 查找目前mysql的用户:select user,host,password from mysql.user;
- 删除mysql的用户:delete from mysql.user where user='yourusername';
- 插入mysql新用户: insert into mysql.user(Host,User,Password) values("localhost","test",password("root")); 在一些mysql版本下,需要这样插入:grant usage on *.* to 'yourusername'@localhost identified by 'yourpassword' with grant option;
- 刷新,使操作生效:flush privileges
三、 修改指定用户(root)密码
- mysql -u root -p
输入密码
set password from root@localhost=password('yourpassword');
四、 为用户授权
-
授权test用户拥有mmall数据库的所有权限:
grant all privileges on mmall.* to test@localhost identified by 'yourpassword';
-
授权test用户拥有指定的部分权限:
grant select,update on mmall.* to test@localhost identified by 'yourpassword';
-
授权test用户拥有所有数据库的某些权限:
grant select,delete,update,create,drop on *.* to test@"%" identified by "yourpassword";
-
给test用户开通外网所有权限:
grant all privileges on mmall.* to test@"%" identified by "yourpassword";
-
给test用户开通指定ip地址的增改查权限
grant select,insert,update on mmall.* to test@'192.11.11.11' identified by "yourpassword";
五、 其它
- 退出mysql:exit
-
查看mysql版本:select version();