1,create databse cookbook
2,给数据库cookbook创建一个用户burness,密码为123456
grant all on cookbook.* to ‘burness’on ‘localhost’ identified by ‘123456’
3,生成一个备份
mysqldump –h localhost –u burness –p cookbook > cookbook.sql
生成的cookbook.sql内容如下:
-- MySQL dump 10.13 Distrib 5.5.25, for Win64 (x86) -- -- Host: localhost Database: cookbook -- ------------------------------------------------------ -- Server version 5.5.25 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `couter` -- DROP TABLE IF EXISTS `couter`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `couter` ( `depth` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `couter` -- LOCK TABLES `couter` WRITE; /*!40000 ALTER TABLE `couter` DISABLE KEYS */; INSERT INTO `couter` VALUES (1); /*!40000 ALTER TABLE `couter` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `limbs` -- DROP TABLE IF EXISTS `limbs`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `limbs` ( `thing` varchar(20) DEFAULT NULL, `legs` int(11) DEFAULT NULL, `arms` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `limbs` -- LOCK TABLES `limbs` WRITE; /*!40000 ALTER TABLE `limbs` DISABLE KEYS */; INSERT INTO `limbs` VALUES ('human',2,2),('insect',6,0),('squid',0,10),('octopus',0,8),('fish',0,0),('centipede',100,0),('table',4,0),('armchair',4,2),('phonograhp',0,1),('tripod',3,0),('Peg Leg Pete',1,2),('space alien',NULL,NULL); /*!40000 ALTER TABLE `limbs` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2014-04-25 20:41:18
4,mysql从文件中读取语句
mysql cookbook < filename
eg:mysql cookbook < limbs.sql
limbs.sql内容如下:
drop table if exists limbs; create table limbs ( thing varchar(20), legs int, arms int ); insert into limbs (thing,legs, arms) values('human',2,2); insert into limbs (thing,legs, arms) values('insect',6,0); insert into limbs (thing,legs, arms) values('squid',0,10); insert into limbs (thing,legs, arms) values('octopus',0,8); insert into limbs (thing,legs, arms) values('fish',0,0); insert into limbs (thing,legs, arms) values('centipede',100,0); insert into limbs (thing,legs, arms) values('table',4,0); insert into limbs (thing,legs, arms) values('armchair',4,2); insert into limbs (thing,legs, arms) values('phonograhp',0,1); insert into limbs (thing,legs, arms) values('tripod',3,0); insert into limbs (thing,legs, arms) values('Peg Leg Pete',1,2); insert into limbs (thing,legs, arms) values('space alien',NULL,NULL);
或者使用source ,eg : source test.sql
5,mysql还能使用pipe使用程序的查询命令
6,输出重定向:-H -X可以生成HTML,XML内容
mysql -u burness –p cookbook -e “select * from limbs where legs=0” >1.txt
mysql –H -u burness –p cookbook -e “select * from limbs where legs=0” >1.html
mysql –X -u burness –p cookbook -e “select * from limbs where legs=0” >1.xml
7,当过长的查询输出到屏幕需要一行来垂直地显示输出使用G代替;
select * from limbsG
8,记录交互式的mysql对话
mysql –u burness –p cookbook --tee=tmp.out cookbook
T tmp.out
记录的命令及显示会输出到tmp.out
来停止记录
tmp.out 内容如下:
Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 12 Server version: 5.5.25 MySQL Community Server (GPL) Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> T tmp.out mysql> use cookbook; Database changed mysql> select * from limbs; +--------------+------+------+ | thing | legs | arms | +--------------+------+------+ | human | 2 | 2 | | insect | 6 | 0 | | squid | 0 | 10 | | octopus | 0 | 8 | | fish | 0 | 0 | | centipede | 100 | 0 | | table | 4 | 0 | | armchair | 4 | 2 | | phonograhp | 0 | 1 | | tripod | 3 | 0 | | Peg Leg Pete | 1 | 2 | | space alien | NULL | NULL | +--------------+------+------+ 12 rows in set (0.00 sec) mysql>
9,在mysql中使用自己定义的变量
@ var_name : value
eg: select @id := cust_id from customers where cust_id=’customer name’;
delete from orders where cust_id=@id;
delete from custers where cust_id=@id;