-- mysql不区分大小写
-- 连接(登陆mysql服务器)
连接远程服务器:mysql -h 192.168.x.x -u root -p回车
连接本地 : mysql -u root -p
退出
quit / exit
--将查询后的数据立起来: \G
--取消当前未完成的操作: \c
--设置字符集
set names gbk;
-- 查看当前库
show databases;
-- 选库
use 库名;
-- 查看当前表
show tables;
-- 查看当前所在库
select database();
更改root 用户的密码
① 使用root 登陆 mysql服务区
mysql -u root -p密码
② 选择mysql数据库
use mysql;
③ 修改 User表中中 root 用户的密码
update user set Password=password('新密码') where User='root';
where 条件是必须的,如果没有,会修改所有用户的密码 (严重!!!)
④ 更新权限 flush privileges;
-- 创建或者修改用户
grant select,insert,update,delete on db.score to lili@'%' IDENTIFIED BY '123'
*.* 表示所有库,所有表
'localhost' 只允许本地登录
grant all on *.* to lili@'localhost' IDENTIFIED BY '123'
-- 1. 如果用户已经存在,则修改该用户的权限或密码
-- 2. 如果用户不存在,则创建之
-- 3. root 用户才有权限创建
-- 4. 网站只有一个超级管理员
grant all on *.* to jingjing@'%' IDENTIFIED BY '123'
-- 删除用户
① 使用root 用户登陆
② 选择mysql数据库 use mysql;
③ 删除指定的普通用户 delete from user where User='用户名';
where 条件是必须的,如果没有,会删除所有用户
④ 更新权限 flush privileges;
-- 如果忘记root密码
① 关闭mysql服务
② 使用安全模式 打开mysql服务 mysqld --skip-grant-tables (不要加分号)
③ 使用root用户不用密码登陆 mysql -u root
④ 登陆后 进入mysql数据库use mysql;
⑤ 更改root密码
update user set Password=password('新密码') where User='root';
⑥ 更新权限flush privileges;
⑦ 退出mysql安全模式 mysqladmin -u root -p shutdown
要求输入刚刚设置的新密码
⑧ 重新开启mysql服务
新建一个数据库:
create database 数据库名
如:
create database if not exists `tablename`;
`` 反引号 一般用于将字段字,库名,表名,这些标识符给引起来,防止名字与 mysql关键字冲突
删除数据库:
drop database `库名`;
选库:
use `库名`;
查看当前库
select database();
int unsigned 无符号整型(不能是负数)
not null 不能为空
auto_increment 自增
primary key 主键
varchar(255) 字符串类型 最大长度为 255
comment '注释内容' 这是表里面的注释 ,设置在最后
设置表引擎:engine
设置字符集 defaut charset = 字符集 utf8 没有横杆
创建表:
create table if not exists `user`(
`id` int unsigned not null auto_increment primary key comment '主键',
`name` varchar(255) not null comment '用户名',
`pass` char(32) not null comment '密码',
`email` varchar(255) not null comment '邮箱',
`tel` int unsigned not null comment '手机',
`addtime` int unsigned not null comment '添加时间'
)engine=innodb default charset=utf8;
插入到user表,插入的字段有() 插入的值是();
位置一一对应
insert into `user`(`name`,`pass`,`email`,`tel`,`addtime`)
values('小小','123','389438@qq.com','1377777777','1453464346');
查询user表所有字段。
select * from user;
查看表结构:
desc `表名`;
备份与还原:
mysqldump -u root -p test user > c:/user.sql 备份test库某个表
mysqldump -u root -p test > c:/g15.sql 备份test库
本文为Davidvivi原创文章,转载无需和我联系,但请注明来自Davidvivi博客weixia.xin 本人微信:ww646904527,备注博客