net stop mysql
net start mysql
mySQL -uroot -p --tee=c:\log.log
show databases;
create database dbname;
drop database dbname;
show databases;
use dbname
show tables
frm表结构,myd表数据,myi表索引
create table user(id int,name varchar(30),password varchar(30));
desc user;
select * from user
insert into user(id,name,password) values(1,"jack","pwd");
delete from user where id=2
update user set name=‘jay‘ where id=1
rename table tbname1 to tbname2
alter table tbname add pass varchar(32) not null default "password" // 添加字段
alter table tbname modify age int not null default 20;
alter table tbname change name newname varchar(30);
alter table tbname drop age;
drop table tbname if exists tbname
----------------------------------------------------------------------------设计
1.数值 int
create table tname(id int);
desc tname
//type int(11) 有符号
create table tname(id int unsigned zerofill);
desc tname
//type int(10) 无符号
//int(3)与长度无关,不够3位时,前面补0,默认不显示
//auto_increment
//not null
//default
//primary key
2.字符串
char(n) //最大255
varchar(n)
text //65535字节
longtext //42亿字节
3.时间
尽量使用时间戳整型保存
奇怪的null值
\s //查看mysql的基本信息
//查看数据库字符编码
show create database dbname
//查看表字符集
show create table tbname
//
set names utf8
desc select * from user where name="jack"\G //查看运行参数
//主键索引
primary key
//普通索引
index in_named
? show //?类似于linux的man
show index from t2; //查看表中所有索引
//添加普通索引
alter table tbname add index in_name(name)
//删除普通索引
alter table t2 drop index in_named
ddl //数据定义语言 create drop alter
dml //数据操作语言 insert update delete
dql //数据查询语言 select
dcl //数据控制语言 grant commit rollback
-------------------------------------------------------------
insert into tbname(name) values("jack");
update tbname set name="wood" where id=1
delete from tbname where id=2;
delete from tbname where id in(1,3.5);
delete from tbname where id=1 or id=3 or id=5
delete from tbname where id>=3 and id<=5;
delete from tbname where id between 3 and 5;
WeiPHP、CakeWX 、 LaneWeChat,we7
-----------------------------------------------------------------
insert into table 插入多条数据
方法1:
insert into `ttt`
select ‘001‘,‘语文‘ union all
select ‘002‘,‘数学‘ union all
select ‘003‘,‘英语‘;
方法2:
INSERT INTO tab_comp VALUES(item1, price1, qty1),
(item2, price2, qty2),
(item3, price3, qty3);
方法3:
INSERT INTO tab_comp(item1, price1, qty1) SELECT item1, price1, qty1 FROM tab_cc;
------------------------------------------------------------------
查询
别名查询
select username name from user
select username as name from user
distinct
select distinct age from user
//
空值null 注意 is null / is not null 不可以用=
select * from user where addr is null
//like 注意%
//regexp
select * from user where name like ‘%s%‘;
select * from user where name regexp ‘.*4.*‘;
select * from user where name like ‘%4%‘ or name like ‘%5%‘;
select * from user where name regexp ‘(.*4.*)|(.*5.*)‘;
//order by asc(升序 默认)/desc(降序)
select * from user order by id
select * from user order by id asc
select * from user order by id desc
//limit
select * from user order by id desc limit 1;//取1个
select * from user order by id desc limit 0,3;//从第0个起取3个
//concat() 连接符
select concat("a","b");//输出ab
select id,name,pass,concat(id,‘-‘,name) from user;
//rand()
select * from user order by rand() limit 3;//随机取出3个
//count()
//sum()
//avg()
//max()
//min()
select count(id) num from user;//统计id的个数,num别名
select count(*) num from user;//统计个数,注意:官方推荐使用
//
select sum(id) num from user;// 求和
select avg(id) num from user;//
//group by having order by 分组不能用where,只能用having
select name ,count(id) from mess group by name; //
select shopId ,count(id) num from `p_purchase_order_his` group by shopId order by num desc;
select name, count(id) tot from mess group by name having tot>=80 order by tot desc; //group by必须在order by前面
//after
alter table tbname add store int after id;
//多表查询
普通查询-多表
select * from tb1 tb2 ;//tb1的整个记录加上tb2的一条条记录
select * from tb1 tb2 where tb1.id=tb2.oid
左连接(把需要完全显示的放到左边) left join on
select * from tb1 left join tb2 on tb1.id=tb2.oid
右连接(把需要完全显示的放到右边) right join on
子查询(嵌套查询):一般情况下不使用
select name from user where id in(select uid from mess);
-------------------------------------------------
php mysql
header("content-type:text/html;charset=utf-8");
[email protected]_connect("localhost","root","root");
if(!conn){
echo mysql_errno().<br/>.mysql_error();
}
mysql_select_db("test");
mysql_query("set names utf8");
mysql>create table t1(id int unsigned auto_increment primary key,username varchar(32),password varchar(32));
//insert
$username="allen";
$password="pwd";
$sql="insert into t1(username,password) values(‘{$username}‘,‘{$password}‘)";//注意单引号
//$sql="insert into t1(username,password) values(‘$username‘,‘$password‘)";//注意单引号
//echo $sql;
//mysql_query($sql);
if(mysql_query($sql)){
echo mysql_insert_id();
}
//update
$sql="update t1 set username=‘user1‘ where id=2";
mysql_query($sql);
//delete
$sql="delete from t1 where id=2";
mysql_query($sql);
//select 结果集
$sql="select username,addr from t1 order by id";
$res=mysql_query($res);// mysql resource
//mysql_fetch_assoc 关联数组
//mysql_fetch_row 索引数组
//mysql_fetch_array 混合数组
//mysql_fetch_object 对象
$row=mysql_fetch_assoc($res);
$row=mysql_fetch_row($res);
$row=mysql_fetch_array($res);
$row=mysql_fetch_object($res);
//
while($row=mysql_fetch_assoc($res)){
echo "<pre>";
print_r($row);
echo "</pre>";
}
------------------------------------------
//取列信息
while($f=mysql_fetch_field($res)){
echo "<pre>";
print_r($f->name);
echo "</pre>";
}
//取列信息
$sql="desc t1";
$res=mysql_query($sql);
while($row=mysql_fetch_assoc($res)){
echo "<pre>";
print_r($row[‘Field‘]);
echo "</pre>";
}
-----------------------------------------------------
mysql_insert_id()//取得上一步insert操作产生的id
mysql_affected_rows();//取得增、删、改操作所影响的行数
mysql_num_rows()//取得查询结果集中行的数目
select count(*) from tbname;//最快获取表的行数
mysql_close($conn);
---------------------------------------------------------------------------------------------
shell mysql
chmod a+x xxx.sh #给xxx.sh执行权限
shell脚本
----
#! /bin/bash
#mysql.sh
mysql="/usr/local/mysql/bin/mysql -uroot -p123"
#sql="show databases"
#sql="create table test.user(id int unsigned,name varchar(32),password varchar(32))"
#sql="desc test.user"
#sql="insert into test.user(username,password) values(‘jack‘,‘pwd‘)"
$mysql -e "$sql"
-----
-----
#! /bin/bash
#mysql.sh
conn="/usr/local/mysql/bin/mysql -uroot -p123"
#sql="create database test"
#sql="create table test.user(id int unsigned auto_increment primary,name varchar(32),password varcher(32))"
#sql="insert into test.user(username,password) values(‘jack‘,‘pwd‘)"
#传参
case $1 in
delete)
sql="delete from test.user where id=$2"
;;
insert)
sql="insert into test.user(username,password) values(‘$2‘,‘$3‘)"
;;
update)
sql="update test.user set username=‘$3‘,password=‘$4‘ where id=$2"
;;
select | *)
sql="select * from test.user order by id"
;;
esac
$conn -e "$sql"
---------------------------------------
shell apache
apache日志分割
rotatelogs,cronolog
#! /bin/bash
#logcut.sh
yesterday=`date -d yesterday +%Y%m%d`
srclog="/usr/local/apache2/logs/access_log"
dstlog="/usr/local/apache2/logsbak/access_${yesterday}"
mv $srclog $dstlog
pkill -1 httpd
------------------------
cat access_xxxxxxxx.log|awk ‘{print $1}‘ |sort|uniq -c
--------------------------
#! /bin/bash
#mysql.sh
conn="/usr/local/mysql/bin/mysql -uroot -p123"
#sql="create table test.countab(id int unsigned auto_increment primary key,data varchar(100),ip varchar(100),num int)"
#sql="desc test.countab"
$conn -e "$sql"
-------
#! /bin.bash
yesterday=`date -d yesterday +%Y%m%d`
srclog="/usr/local/apache2/logs/access_log"
dstlog="/usr/local/apache2/logsbak/access_${yesterday}"
mv $srclog $dstlog
pkill -1 httpd
tmpfile=$$.txt # $$什么鬼
cat access_xxxxxxxx.log|awk ‘{print $1}‘ |sort|uniq -c|awk ‘{print $1":"$2}‘ > $tmpfile
mysql="/usr/local/mysql/bin/mysql -uroot -p123"
for i in `cat $tmpfile`
do
ip=`echo $i|awk -F: ‘{print $1}‘`
num=`echo $i|awk -F: ‘{print $2}`
sql="insert into test.countab(data,ip,num) values(‘$yesterday‘,‘$ip‘,‘$num‘)"
done
rm -rf $tmpfile
sql="select * from test.countab order by id"
$mysql -e "$sql"
-------------------
crontab -e
#每晚0点执行
00 00 * * * /mnt/xxx.sh
---------------------