1、连接到数据库:
mysql -u root -p
或 mysql -u root -h 106.14.72.xxx -p
查看数据库列表: show databases;
列出所有表:show tables;
切换数据库:use 数据库名;
显示数据表结构:describe 表名;
查看用户状态: select host,user from mysql.user;
2、创建用户:
允许本地 IP 访问 localhost, 127.0.0.1
create user 'test'@'localhost' identified by '123456';
允许外网 IP 访问
create user 'test'@'%' identified by '123456';
刷新: flush privileges;
3、建数据库:
create database testDB DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
4、授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
grant all privileges on testDB.* to test@localhost identified by '123456';
指定部分权限给一用户:
grant select,update on testDB.* to test@localhost identified by '123456';
授权test用户拥有所有数据库的某些权限:
grant select,delete,update,create,drop on *.* to test@'%' identified by '123456';
// @'%' 表示对所有非本地主机授权,不包括localhost。
flush privileges; //刷新系统权限表
5、删除用户
Delete FROM mysql.user Where User='test' and Host='%';
删除数据库:
drop database testDB;
删除账户及权限:drop user 用户名@'%';
drop user 用户名@ localhost;
6、修改指定用户密码
update mysql.user set password=password('新密码') where User="test" and Host="localhost";
flush privileges;
7、退出 :exit;
注意:后面的分号;不能少
8、对数据表的一些基本操作
删除数据表:DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...];
清空数据表数据:
自增字段不重置 delete
from
tablename;
自增字段重置 truncate
table
tablename;