登陆之前命令结束符为回车,登陆之后命令结束符为;
或\g
登陆
-
本地登录
开启MySQL服务,切换到MySQL安装目录的bin目录下(建议配置环境变量),mysql -u username -p
,回车然后输入密码 -
远程登陆
mysql -h ip -P 3306 -u username -p密码
(p和密码之间无空格,这样密码可见,也可以先不输密码,回车后输入密码,则密码不可见),-h
是主机名,-P
是端口,-u
是用户名,-p
是密码;这种格式是通用的,不管本地还是远程数据库 -
退出
exit
或quit
或\q
账号管理
数据库账号信息在mysql数据库的user表中,字段Host
、User
和authentication_string
,分别表示账号的主机名(或ip地址,即可以从这个地址登陆到本机,“%”表示任何主机或ip),账号名,密码,还有很多表示权限的字段。对账号的操作实际就是对user表的操作
- 添加账号
//如果切换到了mysql数据库则无需mysql.user,直接user就行
insert into mysql.user(Host,User,authentication_string) values("localhost","test",password("1234"));
password("1234")
是加密密码函数,数据库中保存加密的密码
上面的方法从5.1版本之后就会报错,改用下面的
//添加一个名为luckee密码为111的本地账户
grant usage on *.* to 'luckee'@'localhost' identified by '111' with grant option;
//授权给luckee在test数据库上的所有权限
grant all privileges on test.* to 'luckee'@'localhost' identified by '111';
//刷新权限
flush privileges;
- 账号授权
授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码";
授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):
grant all privileges on testDB.* to test@localhost identified by '1234';
flush privileges;//刷新系统权限表
如果想指定部分权限给一用户,可以这样来写:
grant select,update on testDB.* to test@localhost identified by '1234';
flush privileges; //刷新系统权限表
授权test用户拥有所有数据库的某些权限,@"%" 表示对所有主机授权
grant select,insert,delete,update,create,drop on *.* to test@"%" identified by "1234";
flush privileges; //刷新系统权限表
在使用root连接mysql的时候,主机名指定为localhost和127.0.0.1都可以连接,但是指定为电脑的实际IP地址(如192.168.1.102)则连接不成功
原因就是user表中root账号的Host是localhost
,意思是root账号只能从localhost登录,改成%
就可以了,%
表示能从任何ip地址连接到该mysql服务器,当然也可以指定其他的IP地址
数据库操作
-
show databases;
展示所有数据库 -
create database dbName
新建数据库 -
drop database dbName
删除数据库 -
use dbName
切换数据库
表操作
-
show tables
展示所有表 -
create table tableName
新建表 -
drop table tableName
删除表 -
describe tableName
展示表结构信息 -
alter table tableName add columnName columnType 约束
增加字段 -
alter table tableName drop column columnName
删除字段
网友评论