创建用户
create user 'root'@'%' identified by 'password';
创建用户并赋予指定权限
grant create,select,update,insert,delete,alter on bbs.* to lvtao@localhost identified by 'password';
创建用户并赋予全部权限
grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
创建备份用户
grant select,reload,show databases,lock tables,event,replication client on *.* to 'bak'@'localhost' identified by 'password';
备份所有数据库
mysqldump -u root -p --all-databases --ignore-database=performance_schema --ignore-database=information_schema --skip-lock-tables > /home/db.sql
导出一个数据库结构
mysqldump -u root -p -d –add-drop-table database >/home/db.sql
恢复数据
a:常用source 命令
进入mysql数据库控制台,
如mysql -u root -p
mysql>use 数据库
然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
mysql>source wcnc_db.sql
b:使用mysqldump命令
mysqldump -u username -p dbname < filename.sql
c:使用mysql命令
mysql -u username -p -d dbname < filename.sql
创建数据库
create database <数据库名>;
显示所有的数据库
show databases;
删除数据库
drop database <数据库名>;
选择数据库
use <数据库名>;
查看当前使用的数据库
select database();
当前数据库包含的表信息:
show tables;
建表
create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);
mysql> create table myclass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
获取表结构
desc 表名,或者show columns from 表名
mysql>describe myclass;
mysql>desc myclass;
mysql>show columns from myclass;
删除表
drop table <表名>
mysql> drop table myclass;
插入数据
insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
mysql> insert into myclass values(1,'tom',96.45),(2,'joan',82.99), (2,'wang', 96.59);
查询表中的数据
1)、查询所有行
命令: select <字段1,字段2,...> from < 表名 > where < 表达式 >
例如:查看表 myclass 中所有数据
mysql> select * from myclass;2)、查询前几行数据
例如:查看表 myclass 中前2行数据
mysql> select * from myclass order by id limit 0,2;
或者:
mysql> select * from myclass limit 0,2;
删除表中数据
delete from 表名 where 表达式
mysql> delete from myclass where id=1;
修改表中数据
update 表名 set 字段=新值,… where 条件
mysql> update myclass set name='mary' where id=1;
在表中增加字段:
alter table 表名 add字段 类型 其他;
mysql> alter table myclass add passtest int(4) default '0'
更改表名:
rename table 原表名 to 新表名;
mysql> rename table myclass to youclass;
更新字段内容
update 表名 set 字段名 = 新内容update 表名 set 字段名 = replace(字段名,'旧内容','新内容');
文章前面加入4个空格update article set content=concat('',content);
更新字段部分字符串
update contents set `text`=replace(text,'http://www.lvtao.net','https://www.lvtao.net')
字段:数值类型
字段:字符串型
字段:日期型