服务启动&关闭
net start mysql// win下开启mysql的服务
net stop mysql// win下关闭mysql的服务
#linux下一般需要在root权限下,开启、重启、关闭mysql
mysqladmin start
/ect/init.d/mysql start // 前面为mysql的安装路径
mysqladmin restart
/ect/init.d/mysql restart
mysqladmin shutdown
/ect/init.d/mysql shutdown
登陆&退出
mysql -h 192.168.1.23 -u test -p12345abc trafficdb //p与密码之间不加空格
exit // 退出mysql
quit // 退出mysql
状态
show character set;// 查看当前mysql支持的字符集
show variables like 'character_set%'; // 查看当前mysql使用的字符集设置
show variables like 'collation_%'; // 查看当前mysql使用的校对规则设置
用户
select user(); // 当前登陆用户 root@localhost
show grants; // 查看当前用户的权限
show grants for testuser@localhost; // 查看testuser的用户权限
flush privileges; // 修改完权限后,刷新使其生效
// 新建一个只能从localhost登陆的testuser用户,密码为123456的用户
create user 'testuser'@'localhost' identified by '123456';
// 新建一个只能从localhost登陆的testuser用户,无密码
create user 'testuser'@'localhost' identified by '';
// 删除只能从localhost登陆的testuser用户
drop user 'testuser'@'localhost';
// 用户test1可以从任意一台主机登陆(%表示任意字符串,也就是在任何主机上都可以)
grant select,insert,update,delete on *.* to 'test1'@% identified by abc;
// 删除testuser的数据库操作权限
revoke all privileges on * . * from 'testuser'@'localhost';
// 删除testuser授权给其他用户的权限
revoke grant option on * . * from 'testuser'@'localhost';
// 分配指定权限给testuser用户,并对该用户进行mysql的资源限定
grant select, create,show databases on * . * to 'testuser'@'localhost'
with max_queries_per_hour 1000 max_connections_per_hour 100 max_updates_per_hour 50 max_user_connections 8;
// 设置testuser用户有对trafficdb数据库的操作权限
grant all privileges on `trafficdb` . * to 'testuser'@'localhost';
// 将testuser的密码设置为test123
set password for 'testuser'@'localhost' = password( 'test123' );
// 使用mysqladmin修改mysql中root用户的密码
mysqladmin -h 192.168.1.88 -u root -p password 123456
windows下忘记mysql超级管理员root密码的解决办法
数据库
1. 创建
create dababase trafficdb;
// 创建名为trafficdb的数据库,字符集设置为utf8,校对规则设置为utf8_bin
create dababase `trafficdb` default character set utf8 collate utf8_bin;
mysql字符集和整理列表
注:ci是case insensitive, 即 大小写不敏感, a 和 a 会在字符判断中会被当做一样的;
bin是二进制, a和a会别区别对待.
2. 删除
drop database trafficdb;
drop database if exists trafficdb;
3. 设置
use trafficdb; //设置当前数据库
4. 查看
show databases; // 查看当前用户下所有数据库
select database(); // 显示当前操作数据库
show create dababase trafficdb; // 查看trafficdb的字符集与校对规则设置
5. 修改
alter database trafficdb default character set 'gbk' collate 'gbk_bin'; // 修改trafficdb的字符集为gbk、校对规则为gdb_bin
6. 导出
// 导出trafficdb数据库中,所有建表命令与表中的数据到trafficdb.sql中
mysqldump -h 192.168.1.88 -u root -p trafficdb > trafficdb.sql
// 以gbk编码方式,导出trafficdb数据库中,所有建表命令与表中的数据到trafficdb.sql中
mysqldump -h 192.168.1.88 -u root -p --default-character-set=gbk trafficdb > trafficdb.sql
// 导出trafficdb数据库中,所有建表命令到createtables.sql中
mysqldump -h 192.168.1.88 -u root -p -d trafficdb > createtables.sql
// 导出trafficdb数据库中,所有建表命令(增加若表存在则先删除命令)到createtables.sql中
mysqldump -h 192.168.1.88 -u root -p -d --add-drop-table trafficdb > createtables.sql
// 导出trafficdb数据库中,所有表中的数据到tablesdata.sql中
mysqldump -h 192.168.1.88 -u root -p -t trafficdb > tablesdata.sql
7. 脚本导入
mysqldump -h 192.168.1.88 -u root -p create trafficdb // 创建trafficdb数据库
mysqldump -h 192.168.1.88 -u root -p trafficdb mysql -h 192.168.1.88 -u root -p trafficdb
source /home/dbuser/trafficdb.sql // 在mysql中执行trafficdb.sql脚本
8. 文本导入
(1) 文本数据应符合的格式:字段数据之间用tab键隔开,null值用/n来代替. 例:
3rose深圳二中1976-10-10
4mike深圳一中1975-12-23
(2) 数据传入命令 load data local infile 文件名 into table 表名;
注:最好将文件复制到/mysql/bin目录下,并且要先用use命令打开表所在的库
表
1. 创建
create table `trafficdb`.`road` (
`id` int( 4 ) not null auto_increment primary key comment '主键id',
`name` varchar( 255 ) character set utf8 collate utf8_bin null ,
`post_no` int( 4 ) not null default '23410',
`length` float not null ,
`build_date` date not null ,
`build_time` time not null ,
`date_time` datetime not null ,
`data` blob not null ,
unique (`post_no`) ,
index ( `length` )
);
create table `student` (
`id` tinyint( 255 ) unsigned not null auto_increment primary key ,
`content` varchar( 255 ) not null
) type = myisam character set gbk collate gbk_chinese_ci;
2. 删除
drop table road ;
drop table if exists road ;
3. 清空
truncate table road ;
4. 查看
show tables;
show create table road;//查看road表的字符集与校对规则设置
5. 修改表名
rename table vehicle to driver;
alter table vehicle rename driver;
alter table vehicle default character set 'utf8' collate 'utf8_bin';
6. 表结构
desc road; 【describe road;】//查看表结构
show columns from road; //查看表结构
alter table `road` add `extra` bool null comment '额外说明'; //在末尾增加一个字段extra
alter table `road` add `extra` int( 4 ) not null first; //在表开头添加一个字段extra
// 在字段length后添加一个字段extra
alter table `road` add `extra` char( 1 ) character set ascii collate ascii_bin null after `length`;
alter table `road` drop `extra`; //删除字段extra
alter table `road` change `post_no` `post_no` text null; //修改字段post_no信息
7. 三种索引:主键索引(primary key)、唯一索引(unique)、普通索引(index)
# 若表中有自动增长的字段,若要设置primary key,只能为该字段
alter table `road` drop primary key; //删除road表的主键
alter table `road` add primary key ( `id` ); //将id设为主键
alter table `road` drop primary key , add primary key ( `post_no` ); //删除当前主键,将post_no设为主键
alter table `road` add unique (`post_no`); // 将post_no设置为unique
alter table `road` add index ( `post_no` ); // 将post_no设置为index
show index from road; // 显示road表上所有索引
show keys from road; // 显示road表上所有索引
create index idx1 on road (length); //在road表的名为length字段上添加名为idx1的索引
create unique index uq1 on road (post_no); //在road表的名为post_no字段上添加名为uq1的unique索引
drop index idx1 on road; // 删除名为idx1的索引
alter table road drop index uq1; //删除名为uq1的索引
8. 强制更新表
flush table road;
9. 优化表
optimize table road;
10. 导出
// 导出trafficdb数据库的road表中,所有建表命令与表中的数据到trafficdb.sql中
mysqldump -h 192.168.1.88 -u root -p trafficdb road > trafficdb_road.sql
记录
1. 查询
select * from road;
select * from road /g; // 以列的方式显示查询到的内容
select * from road limit 1, 5 ; // 显示查询到的第1-5行(序号从0开始)
select * from road limit 10 ; // 等价于select * from road limit 0, 10 ;
select id, name, post_no from road order by id desc; // asc表示升序(不写默认为升序),desc表示降序
select count(*) from road; // road表的记录数
select max(length) from road; // road表中所有记录的length字段的最大值
select min(length) from road; // road表中所有记录的length字段的最小值
select sum(length) from road; // road表中所有记录的length字段的之和
select avg(length) from road; // road表中所有记录的length字段的平均值
select * from `qpapers`
where `id` >=11100
and `title` like '%中国%'
and `type` is not null; // 条件查询
select * from `qpapers`
where `id` >=11100
and `type` in ( 0, 1, 2 ); // 条件查询
select count( * ) as `行数`, `type` from `qpapers`
group by `type` order by `type`; // 查询type字段的非重复值
2. 插入
insert into `qss`.`qpapers` (
`id` ,`title` ,`type` ,`status` ,`style_id` ,`owner` ,`url` ,`conn_qp_id` ,`start_time` ,`end_time`)
values ('120', '你好', '3', '2', '0', 'admin', null , null , '2013-05-05 15:46:05', null);
insert into `trafficdb`.`road` (
`id` ,`name` ,`post_no` ,`length` ,`build_date` ,`build_time` ,`date_time` ,`blob_data`)
values ('2', '珞瑜路', '450000', '50.8', '2013-05-05', now( ) , '2013-05-05 15:54:21', 0x3002a00c20010706);
insert into myclass values(1, 'tom', 96.45),(2, 'joan', 82.99),(3, 'wang' 90);
3. 更新
update `qss`.`qpapers` set `end_time` = '2013-05-05 15:41:40' where `qpapers`.`id` =11138 ;
4. 替换
replace into `road` (`id`, `name`, `post_no`, `length`, `build_date`, `build_time`, `date_time`, `blob_data`) values
(2, '珞瑜路', 450000, 50.8, '2013-05-05', '15:58:33', '2013-05-05 15:54:21', 0x3002a00c20010706);
replace into myclass values(1, 'tom', 96.45),(2, 'joan', 82.99),(3, 'wang' 90);
5. 删除
delete from `qss`.`qpapers` where `qpapers`.`id` = 11138 ;
函数
select from_unixtime(1367997752);// 得到2013-05-08 15:22:32
select unix_timestamp(2013-05-08 15:22:32); // 得到1367997752
select unix_timestamp(now()); // 得到1367662473
select current_date(); // 得到当前日期 2013-05-05
select ( 20 +5 ) *4 as result, sin(pi( )/3), now(); // 100 0.86602540378444 2013-05-05 16:45:49
select hex( 'aa我' ); // 查看字符串二进制,得到4161e68891(当前字符集为utf8)
bitscn.com
