﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-kxbin-文章分类-MYSQL</title><link>http://www.blogjava.net/kxbin/category/49611.html</link><description>成功留给有准备的人</description><language>zh-cn</language><lastBuildDate>Wed, 26 Sep 2012 23:46:34 GMT</lastBuildDate><pubDate>Wed, 26 Sep 2012 23:46:34 GMT</pubDate><ttl>60</ttl><item><title>使用INSERT语句向表中插入数据（MSSQLSERVER版）</title><link>http://www.blogjava.net/kxbin/articles/387989.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Tue, 18 Sep 2012 07:09:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/387989.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/387989.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/387989.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/387989.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/387989.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp; &nbsp; &nbsp;做开发的同仁对于向数据库中插入数据可谓是太普通不过了，也没什么说的，一般都是采用常用的INSERT INTO [(字段列表)] VALUES(字段值列表)，这样的方式进行操作。今天，我要给大家介绍的是其他一些非常规的方式，一定会让你耳目一新，眼前一亮。&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 进入今天的...&nbsp;&nbsp;<a href='http://www.blogjava.net/kxbin/articles/387989.html'>阅读全文</a><img src ="http://www.blogjava.net/kxbin/aggbug/387989.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-09-18 15:09 <a href="http://www.blogjava.net/kxbin/articles/387989.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>oracle学习笔记</title><link>http://www.blogjava.net/kxbin/articles/384940.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Mon, 06 Aug 2012 12:56:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/384940.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/384940.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/384940.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/384940.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/384940.html</trackback:ping><description><![CDATA[1.oracle服务器包括一个oracle例程和一个oracle数据库。oracle例程是后台进程和内存结构的组合，只有启动例程后，才能访问数据库中的数据；oracle数据库包括数据库文件，提供实际的物理存储。<br />2.oracle安装会自动生成sys用户和system用户。sys用户是超级用户，具有最高权限，具有sysdba角色。有create database的权限，该用户默认密码是manager；system用户是管理操作员，具有sysoper角色，没有create database的权限，默认密码 change_on_install;对数据库的维护一般用system用户就可以了。<br />3.sql*plus常用命令：<br />&nbsp;1）连接命令<br />&nbsp; &nbsp; conn 用户名/密码@网络服务名 [as sysdba/sysoper]，当用特权用户身份连接时，必须带上as sysdba或是as sysoper;<br />&nbsp; &nbsp; disc[onnect]，用于断开与当前数据库的连接；<br />&nbsp; &nbsp; passw[ord]，用于修改用户的密码，如果要想修改其他用户的密码，需要用sys/system登录；<br />&nbsp; &nbsp; show user 显示当前用户名；<br />&nbsp; &nbsp; exit 断开与数据库的连接，同时退出；<br />&nbsp;2）文件操作命令<br />&nbsp; &nbsp; start和@，运行sql脚本。例如：start d:\a.sql<br />&nbsp; &nbsp; edit，编辑指定的sql脚本<br />&nbsp; &nbsp; spool，将sql*plus屏幕上的内容输出到指定文件中去。例如 spool d:\aa.sql ，并输入 spool off。<br />&nbsp;3）显示和设置环境变量<br />&nbsp; &nbsp; &nbsp;linesize，设置显示行的宽度，默认是80个字符。例如：show linesize，set linesize 90；<br />&nbsp; &nbsp; &nbsp;pagesize，设置每页显示的行数目，默认是14。<br />4.用户管理<br />&nbsp;1）创建用户 create user xiaohuang identified by m123，xiaohuang用户名，m123密码，密码不能以数字开头。具有dba的权限才能创建用户；<br />&nbsp;2）修改密码 如果给自己修改密码直接用 password 用户名；如果给别人修改密码 alter user xiaohuang indentified by m345，必须具有dba权限的人才能修改。<br />&nbsp;3）删除用户，需具有drop user 权限，drop user 用户名 cascade。cascade级联删除，如果已经创建表则一并删除。<br />5.权限管理分为系统权限和对象权限<br />&nbsp;1）系统权限，用户对数据库的相关权限（执行特定类型sql命令的权利，用于控制用户执行一个或一组数据库操作。常见系统权限包括create session 连接数据库；create view 创建视图；create procedure 创建过程、函数、包；create table 创建表）；查询数据字典视图system_privilege_map，获得权限信息；收回系统权限 revoke create session from 用户名1，收回用户名1的连接数据库的权限；<br />&nbsp;2）对象权限，用户对其他用户的数据对象操作的权限。常见对象权限包括alter修改表结构；delete删除数据；update修改数据；select 查询数据；insert插入数据；index在表上建立索引；references 引用；execute执行；<br />&nbsp; &nbsp;对用户访问权限更加精细控制 grant update on emp(sal) to 用户名；回收权限revoke select on emp from 用户名<br />&nbsp;3）对象权限的传递 grant select on emp to 用户名1 with grant option<br />&nbsp;4）系统权限的传递 grant connect to 用户名1 with admin option<br />6.角色，一组权限的集合。分为预定义角色和自定义角色。建立权限的管理<br />&nbsp;1）预定义角色分类：dba拥有全部权限，是系统最高权限，只有dba才可以创建数据库结构；resource只可以创建实体，不可以创建数据库结构；connect可以登录oracle，不可以创建实体，也不可以创建数据库结构。对于普通用户，授予connect，resource权限；对于dba管理用户，授予connect，resource，dba权限。<br />&nbsp;2）自定义角色。建立角色（不验证），如果角色是公用的角色，可以采用不验证的方式建立角色 create role 角色名 not identified;建立角色(数据库验证)，采用这样的方式，角色名、口令存放在数据库中。当激活该角色时，必须提供口令。在建立这种角色时，需要为其提供口令 create role 角色名 identified by shunping.<br />&nbsp; 3）自定义角色授权，当建立角色时，角色没有任何权限；给角色授予权限和给用户授予权限没有太多区别，但是要注意，系统权限的unlimited tablespace和对象权限的with grant option 选项不能授予角色的。grant select on scott.emp to 角色名；grant create session to 角色名 with admin option;<br />&nbsp; 4)把角色赋给用户 grant 角色名 to 用户名 with admin option;<br />&nbsp; 5)删除角色 drop role 角色名<br />7.profile管理用户口令<br />&nbsp;1）profile是口令限制，资源限制的命令集合，当建立数据库时，oracle会自动建立名称为default的profile。当建立用户没有指定profile选项，那oracle会将default分配给用户。<br />&nbsp;2）账号锁定，指定该账户登录时最多可以输入密码的次数，也可以指定用户锁定的时间（天）一般用dba的身份去执行该命令；例如指定tea这个用户最多只能尝试3次登录，锁定时间为2天。创建profile文件，<span style="color: red; ">create profile</span> lock_account <span style="color: red; ">limit failed_login_attempts</span> 3 <span style="color: red; ">password_lock_time</span> 2; <span style="color: red; ">alter use</span><span style="color: red; ">r</span> tea profile lock_account.<br />&nbsp;3）给账号解锁 alter user tea account unlock；<br />&nbsp;4）终止口令<br />&nbsp; &nbsp; &nbsp; 为了让用户定期修改密码可以使用终止口令的指令来完成，同样这个命令也需要dba身份来操作。<br />&nbsp; &nbsp; &nbsp; 例如，给用户tea创建一个profile文件，要求改用户每隔10天要修改自家的登录密码，宽限期为2天。<span style="color: red; ">create profile</span> myprofile <span style="color: red; ">limit password_left_time</span> 10 <span style="color: red; ">password_grace_time</span> 2；alter user tea profile myprofile。<br />&nbsp;5）口令历史<br />&nbsp; &nbsp; &nbsp;如果希望用户在修改密码时，不能使用以前使用过的密码，可使用口令历史，这样oracle就会将口令修改的信息存放到数据字典中，这样当用户修改密码时，oracle就会对新旧密码进行比较，当发现新旧密码一样时，就提示用户重新输入密码。<br />&nbsp; &nbsp; &nbsp;例子：<span style="color: red; ">create profile</span> password_history <span style="color: red; ">limit password_life_time</span> 10 <span style="color: red; ">password_grace_time</span> 2 <span style="color: red; ">password_reuse_time</span> 10。password_reuse_time //指定口令可重用时间即10天后就可以重用。<br />&nbsp;6）删除profile drop profile password_history [<span style="color: red; ">cascade</span>] 级联删除。<br />8.oracle数据类型<br />&nbsp; &nbsp;1.char，定长，最大2000字符，汉字占2个字符。查询效率比varchar类型；插入的数据不足则自动补全；<br />&nbsp; &nbsp;2.varchar，变长，最大4000字符；<br />&nbsp; &nbsp;3.clob，字符型大对象，最大4G；<br />9.保存点 <br />&nbsp; &nbsp;1)savepoint aa 创建保存点；<br />&nbsp; &nbsp;2）rollback to aa 回滚。如果已经提交commit，则保存点不存在，不能再使用回滚。rollback 回滚所有事务;<br />10.删除数据<br />&nbsp; &nbsp;1）delete from student。删除所有记录，表结构还在，写日志，可以恢复的。速度慢；<br />&nbsp; &nbsp;2）drop table student。删除表的结构和数据；<br />&nbsp; &nbsp;3）truncate table student 删除表中的所有记录，表结构还在，不写日志，无法找回删除的记录，速度快；<br />11.只读事务<br />&nbsp; &nbsp; 指只允许执行查询的操作，而不允许执行任何其他dml操作的事务。使用只读事务可以确保用户只能取得某时间点的数据 set transaction read only。<br />12.sys用户和system用户区别<br />&nbsp; 1）存储的数据的重要性不同。<br />&nbsp; &nbsp; &nbsp;sys:所有oracle的数据字典的基表和视图都存放在sys用户中，这些基表和视图对于oracle的运行时至关重要，由数据库自己维护，任何用户都不能手动更改。sys用户拥有dba，sysdba，sysoper角色或权限，是oracle权限最高用户。<br />&nbsp; &nbsp; &nbsp;system：用于存放次一级的内部数据，如oracle的一些特性或工具的管理信息。system用户拥有dba，sysdba角色或系统权限。<br />&nbsp; 2）权限的不同<br />&nbsp; &nbsp; &nbsp;sys用户必须以as sysdba或assysoper形式登录，不能以normal方式登录数据库。<br />&nbsp; &nbsp; &nbsp;system如果正常登录，它其实就是一个普通的dba用户，但是如果以as sysdba登录，其结果实际上它是作为sys用户登录的。<br />13.sysdba和sysoper权限区别<br />&nbsp; &nbsp;系统权限 &nbsp; &nbsp; &nbsp; &nbsp;sysdba &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sysoper<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startup（启动数据库） &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startup&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Shutdown（关闭数据库） &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shutdown<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alter database open/mount/backup &nbsp; &nbsp; alter database open/mount/backup<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: red; ">&nbsp;改变字符集 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 不能</span><br /><span style="color: red; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create database &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;不能创建数据库</span><br /><span style="color: red; ">&nbsp; &nbsp; 区别 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drop database &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 不能删除数据库</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create spfile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;create spfile<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alter database archivelog（归档日志） &nbsp;alter database archivelog<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: red; ">alter database recover(恢复数据库) &nbsp; &nbsp; &nbsp;只能完全恢复,不能执行不完全恢复</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;拥有 restricted session（会话限制） &nbsp; &nbsp; &nbsp; 拥有restricted session权限<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: red; ">&nbsp;可以让用户作为sys用户连接 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;可以进行一些基本的操作，<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;但不能查看用户数据</span><br /><span style="color: red; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;登录之后用户是sys &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;登录之后用户public<br /></span><span>14.逻辑备份<br /></span><span>&nbsp; &nbsp;&nbsp;<span>逻辑备份是指使用工具export将数据对象的结构和数据导出到文件的过程，逻辑恢复是指当数据库对象被误操作而损坏后使用工具import利用备份的文件把数据对象导入到数据库的过程。物理备份即可在数据库open的状态下进行也可在关闭数据库进行，但是逻辑备份和恢复只能在open的状态下进行。<br />15.oracle导出，分为导出表，导出方案，导出数据库方式。<br />&nbsp; &nbsp; &nbsp;导出使用exp命令来完成，该命令常用的选项有：<br />&nbsp; &nbsp; &nbsp; userid：用于指定执行导出操作的用户名，口令，连接字符串；<br />&nbsp; &nbsp; &nbsp; tables：用于指定执行导出操作的表；<br />&nbsp; &nbsp; &nbsp; owner：用于指定执行导出操作的方案；<br />&nbsp; &nbsp; &nbsp; full=y:用于指定执行导出操作的数据库；<br />&nbsp; &nbsp; &nbsp; inctype：用于指定执行导出操作的增量类型<br />&nbsp; &nbsp; &nbsp; rows：用于指定执行导出操作是否要导出表中的数据；rows=n只导出表的结构<br />&nbsp; &nbsp; &nbsp; file：用于指定导出文件名。<br />16.导出表 需要切换到oracle安装目录下的bin目录<br />&nbsp; &nbsp;1）导出自己的表 exp userid=scott/tiger@oracle tables=(emp) file=d:\e1.dmp<br />&nbsp; &nbsp;2)导出其他方案的表。如果用户要导出其他方案的表，则需要dba的权限或是exp_full_database的权限，比如system就可以导出scott的表。exp userid=system/manager@oracle tables=(scott.emp) file=d:\e2.dmp<br />&nbsp; &nbsp;3）导出表的结构 &nbsp; exp userid=scott/tiger@oracle tables=(emp) file=d:\e3.dmp rows=n<br />&nbsp; &nbsp;4)使用直接导出方式 exp userid=scott/tiger@oracle tables=(emp) file=d:\e3.dmp direct=y，这种方式比默认的常规方式速度要快，当数据量大时，可以考虑使用这样的方式，这时需要数据库的字符集要与客户端字符集完全一致，否则会报错。<br />17.oracle导出方案，导出方案是指使用export工具导出一个方案或是多个方案中的所有对象（表，索引，约束...）和数据。并存到文件中<br />&nbsp; 1）导出自己的方案 exp scott/tiger@oracle owner=scott file=d:\scott.dmp<br />&nbsp; 2)导出其他方案 如果用户要导出其他方案，则需要dba的权限或是exp_full_database的权限，例如system用户就可以导出任何方案。 exp system/manager@oracle owner=(system,scott) file=d:\system.dmp。<br />18.oracle导出数据库<br />&nbsp; 1）导出数据库是是指利用export导出所有数据库中的对象及数据，要求改用户具有dba的权限或是exp_full_database权限。 exp userid=system/manager@oracle full=y inctype=complete file=x.dmp。注意：因为数据量大，所以好用的时间会较长。<br />19.导入，导入就是使用工具import将文件中的对象和数据导入到数据库中，但是导入要使用文件必须是export所到处的文件。与导出相似，导入也分为导入表，导入方案，导入数据库三种方式。<br />&nbsp; &nbsp; userid：用于指定执行导入操作的用户名，口令，连接字符串；<br />&nbsp; &nbsp; tables：用于指定执行导入操作的表；<br />&nbsp; &nbsp; fromuser：用于指定源用户；<br />&nbsp; &nbsp; touser：用于指定目标用户；<br />&nbsp; &nbsp; file：用于指定导入文件名；<br />&nbsp; &nbsp; full=y:用于指定执行导入整个文件<br />&nbsp; &nbsp; inctype：用于指定执行导入操作的增量类型<br />&nbsp; &nbsp; rows：指定是否到导入表行（数据）<br />&nbsp; &nbsp; ignore：如果表存在，则只导入数据<br />20.oracle导入表<br />&nbsp; 1）导入自己表 imp userid=scott/tiger@oracle tables=(emp) file=d:\e.dmp<br />&nbsp; 2）导入表到其它用户 要求该用户具有dba的权限，或是imp_full_database<br />&nbsp; &nbsp; &nbsp; &nbsp; imp userid=system/manager@oracle tables=(emp) file=d:/e.dmp touser=scott<br />&nbsp; &nbsp;3）导入表的结构（只导入表的结构而不导入数据）<br />&nbsp; &nbsp; &nbsp; &nbsp; imp userid=scott/tiger@oracle tables=(emp) file=d:/e.dmp rows=n<br />&nbsp; &nbsp;4）导入数据 如果对象（如比表）已经存在可以只导入表的数据<br />&nbsp; &nbsp; &nbsp; &nbsp; imp userid=scott/tiger@oracle tables=(emp) file=d:\e.dmp ignore=y<br />21.oracle导入方案<br />&nbsp; 1）导入自身的方案 imp userid=scott/tiger file=d:\e.dmp<br />&nbsp; 2）导入其它方案（要求该用户具有dba的权限或是imp_full_database权限）<br />&nbsp; &nbsp; &nbsp; &nbsp;imp userid=system/manager file=d:\e.dmp fromuser=system touser=scott<br />22.oracle导入数据库，在默认情况下，当导入数据库时，会导入所有对象结构和数据。<br />&nbsp; &nbsp; imp userid=system/manager@oracle full=y file=d:\e.dmp<br />23.数据字典和动态性能视图<br />&nbsp; 1）数据字典是oracle数据库中最重要的组成部分，它提供了数据的一些系统信息；记录了数据的系统信息，它是只读表和视图的集合，数据字典的所有者为sys用户，用户只能在数据字典上执行查询操作，而其维护和修改由系统自动完成。<br />&nbsp; 2）动态性能视图记载了例程启动后的相关信息。<br />&nbsp; 3）数据字典包括数据字典基表和数据字典视图，其中基表存储数据库的基本信息，普通用户不能直接访问数据字典的基表，数据字典视图是基于数据字典基表所建立的视图，普通用户可以通过查询数据字典视图取得系统信息。数据字典视图主要包括：user_xxx,all_xxx,dba_xxx三种类型。<br />&nbsp; 4）user_tables，用于显示当前用户所拥有的所有表，它只返回用户所对应方案的所有表。例如：select table_name from user_tables;<br />&nbsp; &nbsp;5)all_tables，用于显示当前用户可以访问的所有表，它不仅会返回当前用户方案的所有表，还会返回当前用户可以访问的其他方案的表；<br />&nbsp; &nbsp;6）dba_tables，它会显示所有方案拥有的数据库表，但是查询这种数据库字典视图，要求用户必须是dba角色或是有select_any_table系统权限。例如：当用system用户查询数据字典视图dba_tables时，会返回system,sys,scott...方案所对应的数据库表。<br />24.oracle用户名，权限，角色<br />&nbsp; &nbsp; 在建立用户时，oracle会把用户的信息存放到数据字典中，当给用户授予权限或是角色时，oracle会将权限和角色的信息存放到数据字段。通过查询</span><span style="color: red; ">dba_users</span><span>可以显示所有数据库用户的详细信息；通过查询数据字典视图</span><span style="color: red; ">dba_sys_privs</span><span>，可以显示用户所拥有的系统权限；通过查询数据字典视图</span><span style="color: red; ">dba_tab_privs</span><span>可以显示用户具有的对象权限；通过查询数据字典视图</span><span style="color: red; ">dba_col_privs</span><span>可以显示用户具有的列权限；通过查询数据字典视图</span><span style="color: red; ">dba_role_privs</span><span>可以显示用户所具有的角色；通过查询所有的角色</span><span style="color: red; ">dba_roles</span><span>；查询数据库的表空间</span><span style="color: red; ">dba_tablespaces；简化权限的管理。<br /></span><span>25.如何查询一个角色，包括的权限<br />&nbsp; &nbsp; 1）一个角色包含的系统权限 select * from dba_sys_privs where grantee='CONNECT' 或者 select * from role_sys_privs where role='CONNECT'<br />&nbsp; &nbsp; 2)一个角色包含的对象权限 select * from dba_tab_privs where grantee='CONNECT'<br />26.查询某个用户具有怎样的角色<br />&nbsp; &nbsp; select * from dba_role_privs where grantee='用户名'<br />27.显示当前用户可以访问的所有数据字典视图<br />&nbsp; &nbsp; &nbsp;select * from dict where comments like '%grant%';<br />28.显示当前数据库的全称<br />&nbsp; &nbsp; &nbsp;select * from global_name;<br />29. </span></span><span style="background-color: #ffffff; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; ">查询该用户拥有哪些索引</span><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　select index_name from user_indexes;</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　查询该用户拥有哪些视图</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　select view_name from user_views;</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　查询该用户拥有哪些数据库对象,对象包括表、视图、存储过程、触发器、包、索引、序列、JAVA文件等。</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　select object_name from user_objects;</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　主要描述当前用户的信息，主要包括当前用户名、帐户id、帐户状态、表空间名、创建时间等。</p><p style="margin: 0px; padding: 0px; list-style: none; color: #111111; font-family: 宋体, 微软雅黑, Verdana, Helvetica; font-size: 14px; line-height: 25px; text-align: left; background-color: #ffffff; ">　　select * from user_users;<br /><strong>30.</strong>数据字典分为静态数据字典和动态数据字典。<br />&nbsp; 1）静态数据字典：主要是在用户访问数据字典时不会发生改变的。主要由表和视图组成；<br />&nbsp; 2）动态数据字典：是依赖数据库运行的性能的，反映数据运行的一些内在信息。oracle中这些动态性能视图都是以v$开头的视图；<br />&nbsp; 3）<span style="font-size: 14px; line-height: 25px; ">v$access</span></p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　该视图显示数据库中锁定的数据库对象以及访问这些对象的会话对象（session对象）。</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　select * from v$access</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　v$session</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　该视图列出当前会话的详细信息。</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　v$active_instance</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　该视图主要描述当前数据库下的活动的实例的信息。依然可以使用select语句来观察该信息。</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　v$context</p><p style="margin: 0px; padding: 0px; list-style: none; font-size: 14px; line-height: 25px; ">　　该视图列出当前会话的属性信息。比如命名空间、属性值等<br />31.oracle表空间和数据文件<br />&nbsp; &nbsp;1）表空间是数据库的逻辑组成部分。从物理上讲，数据库数据存放在数据文件中；从逻辑上讲，数据库则存放在表空间中，表空间由一个或多个数据文件组成。<br />&nbsp; &nbsp;2）oracle中逻辑结构包括表空间、段、区、块。数据库由表空间构成，而表空间由段构成，而段由区构成，而区又是由oracle块构成，提高数据库的效率。<br />&nbsp; &nbsp;3）表空间用于从逻辑上组织数据库的数据。数据库逻辑上是由一个或多个表空间组成。通过表空间可以达到以下作用：控制数据库占有的磁盘空间；dba可以将不同数据类型部署到不同的位置，这样有利于提高I/O性能，同时利于备份和恢复等管理操作。<br />32.管理表空间和数据文件<br />&nbsp; &nbsp;1）建立表空间<br />&nbsp; &nbsp; &nbsp;建立表空间是使用create tablespace命令完成，需要注意是，一般情况下，建立表空间是特权用户或是dba来执行的，如果用其他用户来创建表空间，则用户必须要具有create tablespace 的系统权限。<br />&nbsp; &nbsp;2）创建数据表空间<br />&nbsp; &nbsp; &nbsp;在建立数据库后，为便于管理表，最好建立自己的表空间 create tablespace data01 datafile 'd:\test\data01.dbf' size 20m uniform size 128k。在执行完命令后，会建立名称为data01的表空间，并为该表空间建立名称为data01.dbf的数据文件，区的大小128k<br />&nbsp; &nbsp; 3）使用数据表空间<br />&nbsp; &nbsp; &nbsp; create table mypart (deptno number(3),dname varchar2(14)) tablespace data01;<br />&nbsp; &nbsp; 4）改变表空间的状态<br />&nbsp; &nbsp; &nbsp; 当建立表空间时，表空间处于联机的（online）状态，此时该表空间是可以访问的，并且该表空间是可以读写的。但是在进行系统维护或是数据库维护时，可能需要改变表空间的状态，一般由特权用户或是dba来操作。<br />&nbsp; &nbsp; 5）使表空间脱机 alter tablespace users offline;<br />&nbsp; &nbsp; 6）使表空间联机 alter tablespace users online;<br />&nbsp; &nbsp; 7）只读表空间 当建立表空间时，表空间可以读写，如果不希望在该表空间上执行update，delete，insert操作，那么可以将表空间修改为只读 allter tablespace read only或者 read write 。<br />&nbsp; &nbsp; 8）知道表空间名，显示该表空间包括的所有表 select * from all_tables where tablespace_name='表空间名'<br />&nbsp; &nbsp; 9）知道表名，查看该表属于那个表空间 select * from user_tables where table_name='emp'。<br />&nbsp; &nbsp; 10）删除表空间&nbsp;<br />&nbsp; &nbsp; &nbsp; drop tablespace '表空间名' including contents and datafiles;<br />&nbsp; &nbsp; &nbsp; 说明：including contents表示删除表空间时，删除该空间的所有数据库对象，而datafiles 表示将数据库文件也删除。<br />33.oracle 扩展表空间的方式<br />&nbsp; &nbsp;1）增加数据文件 alter tablespace data01 add datafile 'd:\test\data01.dbf' size 20m;<br />&nbsp; &nbsp;2）增减数据文件的大小 alter database datafile 'd:\test\data01.dbf' resize 20m;注意是数据文件的大小不要超过500m;<br />&nbsp; &nbsp;3）设置文件的自动增长 alter database datafile 'd:\test\data01.dbf' autoextend on next 10m maxsize 500m;<br />34.oracle移动数据文件<br />&nbsp; &nbsp; 如果数据文件所在的磁盘损坏时，该数据文件将不能再使用，为了能够重新使用，需要将这些文件的副本移动到其它的磁盘，然后恢复。下面以移动数据文件sp01.dbf为例来说明：<br />&nbsp; &nbsp;1）确定数据文件所在的表空间 select tablespace_name from dba_data_files where file_name='d:\test\sp01.dbf';<br />&nbsp; &nbsp;2）使表空间脱机 alter tablespace sp01 offline;<br />&nbsp; &nbsp;3）使用命令移动数据文件到指定的目标位置 host move d:\test\sp01.dbf c:\test\sp01.dbf<br />&nbsp; &nbsp;4）执行alter tablespace 命令<br />&nbsp; &nbsp; &nbsp;在物理上移动了数据后，还必须执行alter tablespace 命令对数据库文件进行逻辑修改。 alter tablespace sp02 rename datafile 'd:\test\sp01.dbf' to 'c:\test\sp01.dbf';<br />&nbsp; &nbsp;5）使得表空间联机<br />&nbsp; &nbsp; &nbsp;在移动了数据文件后，为了使用户可以访问该表空间，必须将其转变为online状态 alter tablespace data01 online;<br />35.索引<br />&nbsp; &nbsp;用于加速数据存取的数据对象。合理的使用索引可以大大降低I/O次数，从而提高数据访问性能。<br />36.显示角色信息<br />&nbsp;1）显示所有角色信息 select * from dba_roles;<br />&nbsp;2）显示角色所具有的系统权限 select * from role_sys_privs;<br />&nbsp;3）显示角色具有的对象权限 select * from dba_tab_privs;<br />&nbsp;4）显示用户具有的角色及默认角色。当以用户的身份连接到数据库时，oracle会自动的激活默认的角色，通过查询数据字典视图dba_role_privs可以显示某个用户具有的所有角色及当前默认角色。<br />37.oracle精细访问控制<br />&nbsp; 是指用户可以使用函数、策略实现更加细微的安全访问控制。如果使用精细访问控制，则当在客户端发出sql语句(select,insert,update,delete),oracle会自动在sql语句后追加谓词(where 子句)，并执行新的sql语句。通过这样的控制，可以使得不同的数据库用户在访问相同表时，返回不同的数据信息。<br />38.pl/sql（procedural language/sql）是oracle在标准的sql语言上的扩展。pl/sql不仅允许嵌入sql语言，还可以定义变量和常量，允许使用条件语句和循环语句，允许使用例外处理各种错误，这样使得它的功能变得更加强大。<br />&nbsp;1)作用。提高应用程序的运行性能；模块化得设计思想；减少网络传输量；提高安全性；不足：移植性不好；<br />39.执行简单的pl/sql块<br />&nbsp;set serveroutput on -- 打开输出选项<br />&nbsp;begin<br />&nbsp; &nbsp; dbms_output.put_line('hello'); -- 是oracle所提供的包<br />&nbsp;end;<br />40.pl/sql示例<br />&nbsp;declare<br />&nbsp; &nbsp; v_ename varchar2(5); -- 定义字符串变量<br />&nbsp;begin &nbsp;<br />&nbsp; &nbsp; select ename into v_ename from emp where empno=&amp;no;<br />&nbsp; &nbsp; dbms_output.put_line('');<br />&nbsp;end;<br />&nbsp; -- 表示要接收从控制台输入的变量<br />41.过程<br />&nbsp; 过程用于执行特定的操作，既可以制定输入参数，也可以制定输出参数。通过在过程中使用输入参数，可以将数据传递到执行部分；通过使用输出参数，可以将执行部分的数据传递到应用环境。在sqlplus中可以使用create procedure命令建立过程。<br />42. 函数<br />&nbsp; 函数用于返回特定的数据，当建立函数时，在函数头部必须包含return 子句，而在函数体内必须包含return语句返回的数据。create function来建立函数。<br />43. 包<br />&nbsp;包用于在逻辑上组合过程和函数，它由包规范和包体两部分组成。<br />&nbsp;1）可以使用 create package 命令来创建包。<br />&nbsp; &nbsp; &nbsp;create package sp_package is<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; procedure update_sal(name varchar2,newsal number);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function annual_income(name varchar2) return number;<br />&nbsp; &nbsp; &nbsp;end;<br />&nbsp;2）建立包体可以使用create package body<br />44.触发器<br />&nbsp;是指隐含的执行的存储过程。当定义触发器时，必须要指定触发的时间和触发的操作。常用的触发事件包括insert，update，delete语句，而触发操作实际就是一个pl/sql块。可以使用create trigger来建立触发器。</p><img src ="http://www.blogjava.net/kxbin/aggbug/384940.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-08-06 20:56 <a href="http://www.blogjava.net/kxbin/articles/384940.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>通过yum安装mysql</title><link>http://www.blogjava.net/kxbin/articles/383492.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Thu, 19 Jul 2012 03:11:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/383492.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/383492.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/383492.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/383492.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/383492.html</trackback:ping><description><![CDATA[<p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">1. 安装mysql</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; 1.1 执行安装</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: #3366ff; ">$sudo yum -y install mysql-server</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; 1.2&nbsp;<span style="color: #ff0000; ">注意</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql和mysql-server有区别，只需作为client，执行yum install mysql.</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">2. 服务和配置</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; 2.1 开机启动</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: #3366ff; ">$ sudo /sbin/chkconfig --add mysqld</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><span style="color: #3366ff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ sudo /sbin/chkconfig mysqld on &nbsp;&nbsp;</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><span style="color: #3366ff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ sudo /sbin/service mysqld start</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; 2.2 设置root密码</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="color: #3366ff; ">$sudo&nbsp;mysqladmin -u root password '1234'</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">3. 连接mysql</p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp; &nbsp;&nbsp;<span style="color: #3366ff; ">$mysql -h localhost -uroot -p1234</span></p><p style="margin: 0px; padding: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">&nbsp;</p><img src ="http://www.blogjava.net/kxbin/aggbug/383492.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-07-19 11:11 <a href="http://www.blogjava.net/kxbin/articles/383492.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux下自动备份mysql数据库</title><link>http://www.blogjava.net/kxbin/articles/371783.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Tue, 13 Mar 2012 02:37:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/371783.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/371783.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/371783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/371783.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/371783.html</trackback:ping><description><![CDATA[1.创建database.sh文件，内容如下：<br />&nbsp;&nbsp;#!/bin/sh<div>backupDate=`date +"%Y-%m-%d %H:%M:%S"`</div><div>mysqldump -uroot -proot tm&gt;/test/database/tm$backupDate.sql<br />2.创建任务计划</div>&nbsp;&nbsp;[root@localhost]&nbsp;crontab -e<br />&nbsp; 59 23 * * * /test/database/database.sh<img src ="http://www.blogjava.net/kxbin/aggbug/371783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-03-13 10:37 <a href="http://www.blogjava.net/kxbin/articles/371783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Mysql性能优化（转）</title><link>http://www.blogjava.net/kxbin/articles/371649.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Sat, 10 Mar 2012 14:17:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/371649.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/371649.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/371649.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/371649.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/371649.html</trackback:ping><description><![CDATA[<p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><span style="color: #ff0000; ">为了节省篇幅，省略了输出内容，下同。</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">67 rows in set (12.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">只有67行数据返回，却花了12秒，而系统中可能同时会有很多这样的查询，系统肯定扛不住。用desc看一下(注：explain也可)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">desc select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');&nbsp;</span><br />+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+<br />| id | select_type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | type&nbsp;&nbsp; | possible_keys&nbsp;&nbsp; | key&nbsp;&nbsp; | key_len | ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | rows&nbsp;&nbsp;&nbsp; | Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+<br />|&nbsp; 1 | PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | abc_number_prop&nbsp; | ALL&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | NULL&nbsp; | NULL&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;<strong style="font-weight: bold; ">2679838</strong>&nbsp;| Using where&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|&nbsp; 2 | DEPENDENT SUBQUERY | abc_number_phone | eq_ref | phone,number_id | phone | 70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | const,func |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong style="font-weight: bold; ">&nbsp; 1</strong>&nbsp;| Using where; Using index |<br />+----+--------------------+------------------+--------+-----------------+-------+---------+------------+---------+--------------------------+<br />2 rows in set (0.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">从上面的信息可以看出，在执行此查询时会扫描两百多万行，难道是没有创建索引吗，看一下</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">show index from abc_number_phone;&nbsp;</span><br />+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />| Table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Non_unique | Key_name&nbsp;&nbsp;&nbsp; | Seq_in_index | Column_name&nbsp;&nbsp;&nbsp;&nbsp; | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |<br />+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 | PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_phone_id | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 | phone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | phone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 | phone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2 | number_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /><strong style="font-weight: bold; ">| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_id&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |</strong>&nbsp;<br />| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | created_by&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | created_by&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| abc_number_phone |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | modified_by |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | modified_by&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 36879 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; | YES&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+------------------+------------+-------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />6 rows in set (0.06 sec)<br /><br />mysql&gt;&nbsp;<span style="color: #0000ff; ">show index from abc_number_prop;</span>&nbsp;<br />+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />| Table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | Non_unique | Key_name&nbsp;&nbsp;&nbsp; | Seq_in_index | Column_name&nbsp;&nbsp;&nbsp; | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |<br />+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />| abc_number_prop |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 | PRIMARY&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_prop_id | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 311268 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br /><strong style="font-weight: bold; ">| abc_number_prop |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_id&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | number_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 311268 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |</strong>&nbsp;<br />| abc_number_prop |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | created_by&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | created_by&nbsp;&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 311268 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| abc_number_prop |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | modified_by |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 | modified_by&nbsp;&nbsp;&nbsp; | A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 311268 |&nbsp;&nbsp;&nbsp;&nbsp; NULL | NULL&nbsp;&nbsp; | YES&nbsp; | BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+-----------------+------------+-------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+<br />4 rows in set (0.15 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">从上面的输出可以看出，这两张表在number_id字段上创建了索引的。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">看看子查询本身有没有问题。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">desc select number_id from abc_number_phone where phone = '82306839';&nbsp;</span><br />+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+<br />| id | select_type | table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | type | possible_keys | key&nbsp;&nbsp; | key_len | ref&nbsp;&nbsp; | rows | Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+<br />|&nbsp; 1 | SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | abc_number_phone | ref&nbsp; | phone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | phone | 66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | const |&nbsp;<strong style="font-weight: bold; ">&nbsp;&nbsp; 6</strong>&nbsp;| Using where; Using index |<br />+----+-------------+------------------+------+---------------+-------+---------+-------+------+--------------------------+<br />1 row in set (0.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">没有问题，只需要扫描几行数据，索引起作用了。查询出来看看</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">select number_id from abc_number_phone where phone = '82306839';&nbsp;</span><br />+-----------+<br />| number_id |<br />+-----------+<br />|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8585 |<br />|&nbsp;&nbsp;&nbsp;&nbsp; 10720 |<br />|&nbsp;&nbsp;&nbsp; 148644 |<br />|&nbsp;&nbsp;&nbsp; 151307 |<br />|&nbsp;&nbsp;&nbsp; 170691 |<br />|&nbsp;&nbsp;&nbsp; 221897 |<br />+-----------+<br />6 rows in set (0.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">直接把子查询得到的数据放到上面的查询中</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">select * from abc_number_prop where number_id in (8585, 10720, 148644, 151307, 170691, 221897);</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">67 rows in set (0.03 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">速度也快，看来MySQL在处理子查询的时候是不够好。我在MySQL 5.1.42 和 MySQL 5.5.19 都进行了尝试，都有这个问题。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">搜索了一下网络，发现很多人都遇到过这个问题：</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">参考资料1：使用连接（JOIN）来代替子查询(Sub-Queries) mysql优化系列记录<br />http://blog.csdn.net/hongsejiaozhu/article/details/1876181<br />参考资料2：网站开发日记(14)-MYSQL子查询和嵌套查询优化<br />http://dodomail.iteye.com/blog/250199</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">根据网上这些资料的建议，改用join来试试。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><strong style="font-weight: bold; ">修改前：select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><strong style="font-weight: bold; ">修改后：select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">67 rows in set (0.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">效果不错，查询所用时间几乎为0。看一下MySQL是怎么执行这个查询的</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">desc select a.* from abc_number_prop a inner join abc_number_phone b on a.number_id = b.number_id where phone = '82306839';</span>&nbsp;<br />+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+<br />| id | select_type | table | type | possible_keys&nbsp;&nbsp; | key&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | key_len | ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | rows | Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+<br />|&nbsp; 1 | SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | b&nbsp;&nbsp;&nbsp;&nbsp; | ref&nbsp; | phone,number_id | phone&nbsp;&nbsp;&nbsp;&nbsp; | 66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | const&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |&nbsp;&nbsp;&nbsp;<strong style="font-weight: bold; ">&nbsp;6</strong>&nbsp;| Using where; Using index |<br />|&nbsp; 1 | SIMPLE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | a&nbsp;&nbsp;&nbsp;&nbsp; | ref&nbsp; | number_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | number_id | 4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | eap.b.number_id |&nbsp;&nbsp;&nbsp;&nbsp;<strong style="font-weight: bold; ">3&nbsp;</strong>|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+----+-------------+-------+------+-----------------+-----------+---------+-----------------+------+--------------------------+<br />2 rows in set (0.00 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><strong style="font-weight: bold; ">小结：当子查询速度慢时，可用JOIN来改写一下该查询来进行优化。</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">网上也有文章说，使用JOIN语句的查询不一定总比使用子查询的语句快。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">参考资料3：改变了对Mysql子查询的看法<br />http://hi.baidu.com/yzx110/blog/item/e694f536f92075360b55a92b.html</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">正好手头有本《高性能MySQL》，翻阅了一下，第4.4节&#8220;MySQL查询优化器的限制&#8221;4.4.1小节&#8220;关联子查询&#8221;正好讲到这个问题。</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">MySQL有时优化子查询很差，特别是在WHERE从句中的IN()子查询。像上面我碰到的情况，其实我的想法是MySQL会把</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><strong style="font-weight: bold; ">select * from abc_number_prop where number_id in (select number_id from abc_number_phone where phone = '82306839');</strong></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">变成下面的样子</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">select * from abc_number_prop where number_id in (8585, 10720, 148644, 151307, 170691, 221897);</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">但不幸的是，实际情况正好相反。MySQL试图让它和外面的表产生联系来&#8220;帮助&#8221;优化查询，它认为下面的exists形式更有效率</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><strong style="font-weight: bold; ">select * from abc_number_prop where exists (select * from abc_number_phone where phone = '</strong><strong style="font-weight: bold; ">82306839'</strong>&nbsp;<strong style="font-weight: bold; ">and number_id = abc_number_prop.number_id);<br /></strong></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">select * from abc_number_prop where exists (select * from abc_number_phone where phone = '82306839' and number_id = abc_number_prop.number_id);</span></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">67 rows in set (10.89 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; "><br /></p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">mysql&gt;&nbsp;<span style="color: #0000ff; ">desc select * from abc_number_prop where exists (select * from abc_number_phone where phone = '82306839' and number_id = abc_number_prop.number_id);&nbsp;</span><br />+----+--------------------+------------------+--------+-----------------+-------+---------+-------------------------------------+---------+--------------------------+<br />| id | select_type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | table&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | type&nbsp;&nbsp; | possible_keys&nbsp;&nbsp; | key&nbsp;&nbsp; | key_len | ref&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | rows&nbsp;&nbsp;&nbsp; | Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+----+--------------------+------------------+--------+-----------------+-------+---------+-------------------------------------+---------+--------------------------+<br />|&nbsp; 1 | PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | abc_number_prop&nbsp; | ALL&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | NULL&nbsp; | NULL&nbsp;&nbsp;&nbsp; | NULL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<strong style="font-weight: bold; ">2660707&nbsp;</strong>| Using where&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|&nbsp; 2 | DEPENDENT SUBQUERY | abc_number_phone | eq_ref | phone,number_id | phone | 70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; | const,eap.abc_number_prop.number_id |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong style="font-weight: bold; ">1&nbsp;</strong>| Using where; Using index |<br />+----+--------------------+------------------+--------+-----------------+-------+---------+-------------------------------------+---------+--------------------------+<br />2 rows in set (0.01 sec)</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">这种in子查询的形式，在外部表（比如上面的abc_number_prop）数据量较大的时候效率是很差的。（如果对于较小的表，不会造成显著地影响）</p><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #ffffff; ">文中说到<strong style="font-weight: bold; ">一种优化方式就是，手工将in里面的子查询查询出来，然后再拼装执行</strong>&nbsp;，这在程序中是可行的。</p><img src ="http://www.blogjava.net/kxbin/aggbug/371649.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-03-10 22:17 <a href="http://www.blogjava.net/kxbin/articles/371649.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux系统下MYSQL备份与恢复</title><link>http://www.blogjava.net/kxbin/articles/368352.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 11 Jan 2012 09:12:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/368352.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/368352.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/368352.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/368352.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/368352.html</trackback:ping><description><![CDATA[<div><div>本文介绍了linux下如何备份与恢复mysql数据库。</div> <div></div> <div>数据库备份是非常重要的。如果定期做好备份，这样就可以在发生系统崩溃时恢复数据到最后一次正常的状态，把损失减小到最少。</div> <div></div> <div>一、　用命令实现备份</div> <div></div> <div>MySQLl提供了一个mysqldump命令，我们可以用它进行数据备份。</div> <div></div> <div>按提示输入密码，这就把tm数据库所有的表结构和# mysqldump -u root -p tm &gt;  tm_050519.sql数据备份到tm_050519.sql了，因为要总进行备份工作，如果数据量大会占用很大空间，这时可以利用gzip压缩数据，命令如下：</div> <div><pre># mysqldump -u root -p tm | gzip &gt; tm_050519.sql.gz</pre></div> <div></div> <div>系统崩溃，重建系统时，可以这样恢复数据：</div> <div><pre># mysqldump -u root -p tm &lt; tm_050519.sql</pre></div> <div></div> <div>从压缩文件直接恢复：</div> <div><pre>#gzip &lt; tm_050519.sql.gz | mysqldump -u root -p tm</pre></div> <div></div> <div>当然，有很多MySQL工具提供更直观的备份恢复功能，比如用phpMyAdmin就很方便。但我认为，mysqldump是最基本、最通用的。</div> <div></div> <div>二、利用crontab，系统每天定时备份mysql数据库</div> <div></div> <div>利用系统crontab来定时执行备份文件，按日期对备份结果进行保存，达到备份的目的。</div> <div></div> <div>1、创建保存备份文件的路径/var/backup/mysqlbak</div> <div><pre># mkdir -p /var/backup/mysqlbak</pre></div> <div></div> <div>2、创建/usr/sbin/bakmysql文件</div> <div></div> <div>#vi /usr/sbin/bakmysql.sh</div> <div><pre>#!/bin/bash # mysql备份脚本 cd /var/backup/mysqlbak/ dateDIR=`date +"%y-%m-%d"` mkdir -p $dateDIR/data for i in `/usr/local/www/mysql/bin/mysql -uroot -plin31415926 -e "show databases"  |  grep -v "Database" | grep -v "information_schema"` do   /usr/local/www/mysql/bin/mysqldump -uroot -plin31415926 $i   |  gzip  &gt; /var/backup/mysqlbak/$dateDIR/${i}_${dateDIR}.gz done</pre></div> <div></div> <div>3、修改文件属性，使其可执行</div> <div><pre># chmod +x /usr/sbin/bakmysql</pre></div> <div></div> <div>4、修改/etc/crontab</div> <div><pre># crontab -e 在下面添加 01 3 * * * root /usr/sbin/bakmysql</pre></div></div><img src ="http://www.blogjava.net/kxbin/aggbug/368352.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-01-11 17:12 <a href="http://www.blogjava.net/kxbin/articles/368352.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>在linux下安装mysql</title><link>http://www.blogjava.net/kxbin/articles/368347.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 11 Jan 2012 08:37:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/368347.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/368347.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/368347.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/368347.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/368347.html</trackback:ping><description><![CDATA[<div><p>一.下载mysql-5.1.45.tar.gz<br /><a href="http://dev.mysql.com/downloads">http://dev.mysql.com/downloads</a>  <br />或其它地方下载</p> <p>二.添加mysql用户及mysql用户组，并把mysql用户放到mysql用户组中<br />useradd  mysql<br />等与用下面两条命令：<br />groupadd mysql <br />useradd -g mysql mysql</p> <p>三.上传到/usr/local/src下（方便管理）解压安装<br />[root@localhost src]# tar -zxvf  mysql-5.1.45.tar.gz <br />[root@localhost src]# cd mysql-5.1.45</p> <p>1.编译<br />[root@localhost mysql-5.1.45]# ./configure --prefix=/usr/local/mysql  --with-charset=gbk --with-collation=gbk_chinese_ci --with-extra-charsets=all  --with-named-curses-libs=/usr/lib/libncurses.so.5<br />2.安装<br />[root@localhost  mysql-5.1.45]# make &amp;&amp; make install (这个时间比较长)</p> <p>3.将mysql加入到系统变量path中<br />将/usr/local/mysql/bin  加入到path中，这样在shell里就可以运行mysql相关的命令。<br />[root@localhost mysql-5.1.45]# vi  /etc/profile<br />加入<br />export MYSQL_HOME=/usr/local/mysql<br />export  PATH=$MYSQL_HOME/bin:$PATH</p> <p>如果同时安装tomcat可以采用下面的方式<br />export  TOMCAT_HOME=/home/coomix/apache-tomcat-5.5.27<br />export  MYSQL_HOME=/usr/local/mysql<br />export  PATH=$MYSQL_HOME/bin:TOMCAT_HOME/bin:$PATH</p> <p>[root@localhost mysql-5.1.45]# source /etc/profile 使配置文件立即生效</p> <p>4.进入到mysql文件夹下，初始化mysql表格，安装 mysql 默认数据库 <br />[root@localhost mysql-5.1.45]#  cd /usr/local/mysql/<br />[root@localhost mysql]# ls<br />bin docs include lib  libexec mysql-test share sql-bench<br />[root@localhost mysql]# mysql_install_db  --user=mysql</p> <p>5.修改目录权限<br />[root@localhost mysql]# chown -R root:mysql . <br />[root@localhost  mysql]# chown -R mysql var<br />[root@localhost mysql]# chgrp -R mysql .</p> <p>6.修改配置，将mysql加入系统服务： <br />[root@localhost mysql]# cp  /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf<br />[root@localhost  mysql]# cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld  <br />[root@localhost mysql]# chkconfig --add mysqld</p> <p>&nbsp;</p> <p>7.启动mysql <br />[root@localhost mysql]# service mysqld<br />Usage:  /etc/init.d/mysqld {start|stop|restart|reload|force-reload|status} [ MySQL  server options ]<br />[root@localhost mysql]# service mysqld start<br />Starting  MySQL.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [确定]<br />[root@localhost  mysql]#</p> <p>8.使用mysql，默认root用户是没有密码的<br />[root@localhost mysql]# cd bin<br />[root@localhost  bin]# ./mysql -u root -p<br />Enter password: 直接按回车<br />Welcome to the MySQL  monitor. Commands end with ; or \g.<br />Your MySQL connection id is 2<br />Server  version: 5.1.45-log Source distribution</p> <p>Type 'help;' or '\h' for help. Type '\c' to clear the current input  statement.</p> <p>9.修改root密码<br />[root@localhost bin]# mysqladmin -u root password  'root'<br />[root@localhost bin]# mysqladmin -u root -h host_name password  'root'<br />mysqladmin: connect to server at 'host_name' failed<br />error: 'Unknown  MySQL server host 'host_name' (1)'<br />Check that mysqld is running on host_name  and that the port is 3306.<br />You can check this by doing 'telnet host_name  3306'<br />[root@localhost bin]# ./mysql -u root -p<br />Enter password:  输入root<br />mysql&gt; show databases;<br />+--------------------+<br />|  Database&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+--------------------+<br />| information_schema |<br />|  mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| test&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />+--------------------+<br />3  rows in set (0.01 sec)</p> <p>mysql&gt; use mysql;<br />Database changed<br />mysql&gt; show  tables;<br />+---------------------------+<br />| Tables_in_mysql&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  |<br />+---------------------------+<br />| columns_priv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  db&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  func&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| general_log&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  help_category&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| help_keyword&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  help_relation&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| help_topic&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  host&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| ndb_binlog_index&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  plugin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| proc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  procs_priv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| servers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  slow_log&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| tables_priv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  time_zone&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| time_zone_leap_second&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  time_zone_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />| time_zone_transition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; |<br />|  time_zone_transition_type |<br />| user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  |<br />+---------------------------+<br />23 rows in set (0.00 sec)</p> <p>mysql&gt; quit<br />Bye<br />[root@localhost bin]#</p> <p><br />附：[root@localhost mysql]# mysql_install_db  --user=mysql执行后的显示：<br />Installing MySQL system tables...<br />OK<br />Filling help  tables...<br />OK</p> <p>To start mysqld at boot time you have to copy<br />support-files/mysql.server  to the right place for your system</p> <p>PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !<br />To do so,  start the server, then issue the following commands:</p> <p>/usr/local/mysql/bin/mysqladmin -u root password  'new-password'<br />/usr/local/mysql/bin/mysqladmin -u root -h  localhost.localdomain password 'new-password'</p> <p>Alternatively you can  run:<br />/usr/local/mysql/bin/mysql_secure_installation</p> <p>which will also give you the option of removing the test<br />databases and  anonymous user created by default. This is<br />strongly recommended for  production servers.</p> <p>See the manual for more instructions.</p> <p>You can start the MySQL daemon with:<br />cd /usr/local/mysql ;  /usr/local/mysql/bin/mysqld_safe &amp;</p> <p>You can test the MySQL daemon with mysql-test-run.pl<br />cd  /usr/local/mysql/mysql-test ; perl mysql-test-run.pl</p></div><img src ="http://www.blogjava.net/kxbin/aggbug/368347.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-01-11 16:37 <a href="http://www.blogjava.net/kxbin/articles/368347.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Linux下安装mysql</title><link>http://www.blogjava.net/kxbin/articles/368125.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Mon, 09 Jan 2012 03:33:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/368125.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/368125.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/368125.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/368125.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/368125.html</trackback:ping><description><![CDATA[# chmod 755 mysql-5.0.45.tar.gz <br /># tar -zxvf mysql-5.0.45.tar.gz -C /usr/src/ <br /># cd /usr/src/mysql-5.0.45 <br /># ./configure --prefix=/usr/local/mysql --with-charset=gbk --localstatedir=/usr/local/mysql/data <br /># make <br /># make install <br /># groupadd mysql <br /># useradd mysql -g mysql <br /># cd /usr/local/mysql/bin <br /># ./mysql_install_db --user=mysql <br /># cd /usr/local/mysql <br /># chown -R root .<br /># chown -R mysql data <br /># chown -R mysql data/ .<br /># chown -R mysql data/mysql/ . <br /># chgrp -R mysql . <br /># /usr/local/mysql/bin/mysqld_safe --user=mysql &amp; <br /># netstat -tnl |grep 3306 <br /># /usr/local/mysql/bin/mysql -uroot -p <br /># show database; <br /># /usr/local/mysql/bin/mysqladmin -uroot password 6220104 <br /># cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysqld <br /># cd /etc/init.d/ <br /># chmod +x mysqld <br /># chkconfig mysqld <br /># chkconfig --add mysqld<br /># chkconfig --level 345 mysqld on <br /># service mysqld stop <br /># cp /usr/local/mysql/bin/* /usr/bin/ <br />到此mysql-5.0.45 安装完成， <img src ="http://www.blogjava.net/kxbin/aggbug/368125.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2012-01-09 11:33 <a href="http://www.blogjava.net/kxbin/articles/368125.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于 No operations allowed after connection closed.错误</title><link>http://www.blogjava.net/kxbin/articles/365775.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 07 Dec 2011 08:24:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/365775.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/365775.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/365775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/365775.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/365775.html</trackback:ping><description><![CDATA[<div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 25px; text-align: left; background-color: #efefef; "></p><div><div></div><div><div><div><div>ERROR - No operations allowed after connection closed.</div><div>2011-12-07 11:36:09 - ERROR - query failed</div><div>org.hibernate.exception.JDBCConnectionException: could not execute query</div><div><span style="white-space:pre">	</span>at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)</div><div><span style="white-space:pre">	</span>at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)</div><div><span style="white-space:pre">	</span>at org.hibernate.loader.Loader.doList(Loader.java:2148)</div><div><span style="white-space:pre">	</span>at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)</div><div><span style="white-space:pre">	</span>at org.hibernate.loader.Loader.list(Loader.java:2024)</div><div><span style="white-space:pre">	</span>at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)</div><div><span style="white-space:pre">	</span>at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)</div><div><span style="white-space:pre">	</span>at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)</div><div><span style="white-space:pre">	</span>at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)</div><div><span style="white-space:pre">	</span>at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)</div><div>分析，出现这种异常的原因是：</div><div>Mysql服务器默认的&#8220;wait_timeout&#8221;是8小时，也就是说一个connection空闲超过8个小时，Mysql将自动断开该connection。这就是问题的所在，在C3P0 pools中的connections如果空闲超过8小时，mysql将其断开，而C3P0并不知道该connection已经失效，如果这时有Client请求connection，C3P0将该失效的Connection提供给Client，将会造成上面的异常。</div><div>a)解决的方法有3种：</div><div>&nbsp; &nbsp; 增加wait_timeout的时间。</div><div>&nbsp; &nbsp; 减少Connection pools中connection的lifetime。</div><div>&nbsp; &nbsp; 测试Connection pools中connection的有效性。</div><div>b)具体解决方案：<br />&nbsp; &nbsp;1、换一下JDBC驱动，JDBC3.1.0-alpha及以前版本会出现此问题，下载新的JDBC驱动</div><div><span class="Apple-style-span" style="white-space: pre;">&nbsp;  </span>2、使用hibernate配置：</div><div>使用hibernate：&nbsp;</div><div>&lt;property name="connection.autoReconnect"&gt;true&lt;/property&gt;　　&lt;!--这个是最主要的--&gt;&nbsp;</div><div>&lt;property name="connection.autoReconnectForPools"&gt;true&lt;/property&gt;&nbsp;</div><div>&lt;property name="connection.is-connection-validation-required"&gt;true&lt;/property&gt;&nbsp;</div><div>加入以上property，可解决此问题，如果未使用hibernate等持久化管理框架，可在mysql的url中加入autoReconnect=true，这样就可以解决。</div><div>原因很简单。在对数据库操作结束后关闭连接是正确的做法，没什么大问题。至于出现：No operations allowed after connection closed。这样的问题原因只有一个，你这里和数据库的连接Connection是一个Static的，程序共享这一个Connection。所以第一次对数据库操作没问题，当把Connection关闭后，第二次还想操作数据库时Connection肯定不存在了。</div></div></div></div></div></div><img src ="http://www.blogjava.net/kxbin/aggbug/365775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-12-07 16:24 <a href="http://www.blogjava.net/kxbin/articles/365775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql恢复/备份命令</title><link>http://www.blogjava.net/kxbin/articles/364683.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Thu, 24 Nov 2011 00:05:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/364683.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/364683.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/364683.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/364683.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/364683.html</trackback:ping><description><![CDATA[<div><div>1.备份命令</div><div><div><div>mysqldump -u root -p --default-character-set=utf8 itsm&gt;itsm.sql&nbsp;</div><div>2.恢复命令</div><div>mysql -uroot -p --default-character-set=utf8 itsm&lt;e:\pole.sql</div></div></div></div><img src ="http://www.blogjava.net/kxbin/aggbug/364683.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-11-24 08:05 <a href="http://www.blogjava.net/kxbin/articles/364683.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cross ' at line 1 问题</title><link>http://www.blogjava.net/kxbin/articles/364608.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 23 Nov 2011 00:58:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/364608.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/364608.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/364608.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/364608.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/364608.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 使用的SQL：select * from cross。出现上述原因是cross是mysql关键字。mysql关键字列表：ADDALLALTERANALYZEANDASASCASENSITIVEBEFOREBETWEENBIGINTBINARYBLOBBOTHBYCALLCASCADECASECHANGECHARCHARACTERCHECKCOLLATECOLUMNCONDITIONCONNECTI...&nbsp;&nbsp;<a href='http://www.blogjava.net/kxbin/articles/364608.html'>阅读全文</a><img src ="http://www.blogjava.net/kxbin/aggbug/364608.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-11-23 08:58 <a href="http://www.blogjava.net/kxbin/articles/364608.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>数据库的定时备份</title><link>http://www.blogjava.net/kxbin/articles/360346.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Mon, 10 Oct 2011 01:46:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/360346.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/360346.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/360346.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/360346.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/360346.html</trackback:ping><description><![CDATA[<div><div>@echo off&nbsp;</div><div>rem &nbsp;&#9487;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9491;</div><div>rem &nbsp;&#9507;----------------------提示:-----------------------------------&#9515;</div><div>rem &nbsp;&#9507;--该批处理为数据库备份操作文件.-------------------------------&#9515;</div><div>rem &nbsp;&#9507;--由于sqlserver自带备份策略,无需使用批处理.-------------------&#9515;</div><div>rem &nbsp;&#9507;--所以对应数据库类型仅包涵oracle,mysql两种.-------------------&#9515;</div><div>rem &nbsp;&#9507;--存放目录格式如d:\backup\crmnew------------------------------&#9515;</div><div>rem &nbsp;&#9507;--盘符下一级为总目录的文件夹名 例如backup---------------------&#9515;</div><div>rem &nbsp;&#9507;--二级为使用该数据库的项目名称 例如crmnew---------------------&#9515;</div><div>rem &nbsp;&#9507;--执行环境为windows server 2003-------------------------------&#9515;</div><div>rem &nbsp;&#9507;--编写者:CWCA-研发部-刘洋-2011-7-25---------------------------&#9515;</div><div>rem &nbsp;&#9495;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9499;</div><div></div><div>rem &#9487;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9491;</div><div>rem &nbsp; ----------------------初始设置项----------------------------</div><div>rem &nbsp; 设置项目库备份文件存放根目录-脚本自动创建</div><div>set &nbsp; bak_dir=d:\databak</div><div>rem &nbsp; 设置项目名称</div><div>set &nbsp; project_name=cwcanew</div><div>rem &nbsp; 选择备份数据所使用的压缩软件</div><div>set &nbsp; compressFile=c:\uncompress\WinRAR\Rar.exe</div><div>rem &nbsp; 设置数据库的类型,仅支持oracle和mysql</div><div>set &nbsp; db_type=oracle</div><div>rem &nbsp; 设置登陆目的数据库的标识,oracle为sid,mysql为库名</div><div>set &nbsp; db_name=oracle</div><div>rem &nbsp; 设置登陆目的数据库的用户名</div><div>set &nbsp; db_user=****</div><div>rem &nbsp; 设置登陆目的数据库的用户密码</div><div>set &nbsp; db_pwd=****</div><div>rem &nbsp; 设置备份周期,1为按天备份,7为按周备份</div><div>set &nbsp; bak_cycle=1</div><div>rem &nbsp; 设置每个备份文件的保留天数,为0则表示不删除历史文件</div><div>set &nbsp; keep_days=0</div><div>rem &#9495;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9499;</div><div></div><div>rem &#9487;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9491;</div><div>rem &nbsp; ----------------------可选设置项----------------------------</div><div>rem &nbsp; 获取当天为星期几</div><div>set &nbsp; pweek=%date:~-1%</div><div>rem &nbsp; 设置单个备份文件名-此处按"项目名称_日期"命名-例:crmnew_20110101 -建议不要更改</div><div>set &nbsp; filename=%project_name%_%Date:~0,4%年%Date:~5,2%月%Date:~8,2%日</div><div>rem &#9495;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9499;</div><div></div><div>rem &#9487;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9491;</div><div>rem &nbsp; ----------------------------------------执行代码区域----------------------------------------</div><div>if not exist %bak_dir%\%project_name% mkdir %bak_dir%\%project_name%</div><div>set &nbsp; bakfile=%bak_dir%\%project_name%\%filename%</div><div>set &nbsp; filesuffix=</div><div>&nbsp; &nbsp; &nbsp; if %db_type% equ oracle set filesuffix=dmp</div><div>&nbsp; &nbsp; &nbsp; if %db_type% equ mysql set filesuffix=sql</div><div>&nbsp; &nbsp; &nbsp; if %bak_cycle% equ 7 (</div><div>&nbsp; &nbsp; &nbsp; &nbsp;if %pweek% equ 日 goto backup</div><div>&nbsp; &nbsp; &nbsp; &nbsp;goto end</div><div>&nbsp; &nbsp; &nbsp; )&nbsp;</div><div>&nbsp; &nbsp; &nbsp; if %bak_cycle% equ 1 goto backup</div><div>&nbsp; &nbsp; &nbsp; goto end</div><div>&nbsp; &nbsp; &nbsp; :backup</div><div>&nbsp; &nbsp; &nbsp; if %db_type% equ oracle goto backup_oracle</div><div>&nbsp; &nbsp; &nbsp; if %db_type% equ mysql goto backup_mysql</div><div>&nbsp; &nbsp; &nbsp; goto end</div><div>&nbsp; &nbsp; &nbsp; :backup_oracle</div><div>&nbsp; &nbsp; &nbsp; exp %db_user%/%db_pwd%@%db_name% file=%bakfile%.%filesuffix%</div><div>&nbsp; &nbsp; &nbsp; goto compress</div><div>&nbsp; &nbsp; &nbsp; :backup_mysql</div><div>&nbsp; &nbsp; &nbsp; mysqldump -u%db_user% -p%db_pwd% %db_name% &gt; %bakfile%.%filesuffix%</div><div>&nbsp; &nbsp; &nbsp; goto compress</div><div>&nbsp; &nbsp; &nbsp; :compress</div><div>&nbsp; &nbsp; &nbsp; %compressFile% a %bakfile%.rar %bakfile%.%filesuffix%</div><div>&nbsp; &nbsp; &nbsp; goto delete_source</div><div>&nbsp; &nbsp; &nbsp; :delete_source</div><div>&nbsp; &nbsp; &nbsp; del %bakfile%.%filesuffix%</div><div>&nbsp; &nbsp; &nbsp; goto delete_history</div><div>&nbsp; &nbsp; &nbsp; :delete_history</div><div>&nbsp; &nbsp; &nbsp; if %keep_days% neq 0 forfiles /p %bak_dir%\%project_name%\ /d -%keep_days% /c "cmd /c echo deleting @file ... &amp;&amp; del /f @path"</div><div>&nbsp; &nbsp; &nbsp; goto end</div><div>&nbsp; &nbsp; &nbsp; :end</div><div>set &nbsp; bak_dir=</div><div>set &nbsp; project_name=</div><div>set &nbsp; compressFile=</div><div>set &nbsp; db_type=</div><div>set &nbsp; db_name=</div><div>set &nbsp; db_user=</div><div>set &nbsp; db_pwd=</div><div>set &nbsp; bak_cycle=</div><div>set &nbsp; keep_days=</div><div>set &nbsp; pweek=</div><div>set &nbsp; filename=</div><div>set &nbsp; bakfile=</div><div>set &nbsp; filesuffix=</div><div>rem &#9495;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9473;&#9499;</div><div>pause</div></div><img src ="http://www.blogjava.net/kxbin/aggbug/360346.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-10-10 09:46 <a href="http://www.blogjava.net/kxbin/articles/360346.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>测试数据库的默认最大连接数</title><link>http://www.blogjava.net/kxbin/articles/358775.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Fri, 16 Sep 2011 03:00:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/358775.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/358775.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/358775.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/358775.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/358775.html</trackback:ping><description><![CDATA[<div><div>import java.io.IOException;</div><div>import java.sql.Connection;</div><div>import java.sql.DriverManager;</div><div>import java.sql.ResultSet;</div><div>import java.sql.SQLException;</div><div>import java.sql.Statement;</div><div></div><div>/**</div><div>&nbsp;* 测试的最大连接数<br />&nbsp;* <span style="color: red; ">oracle的默认连接数为135</span><br />&nbsp;* <span style="color: red; ">Mysql的默认连接数为100</span></div><div>&nbsp;* @author kxbin</div><div>&nbsp;*&nbsp;</div><div>&nbsp;*/</div><div>class testMaxConn {</div><div><span style="white-space:pre">	</span>int count = 0; // 连接数</div><div><span style="white-space:pre">	</span>Connection[] conn = new Connection[1000];</div><div><span style="white-space:pre">	</span>Statement[] stmt = new Statement[1000];</div><div><span style="white-space:pre">	</span>ResultSet[] rs = new ResultSet[1000];</div><div></div><div><span style="white-space:pre">	</span>/**</div><div><span style="white-space:pre">	</span> * 数据库连接方法（oracle）</div><div><span style="white-space:pre">	</span> */</div><div><span style="white-space:pre">	</span>public void testOracleMaxConn() {</div><div><span style="white-space:pre">		</span>try {</div><div><span style="white-space:pre">			</span>Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();</div><div><span style="white-space:pre">			</span>String url = "jdbc:oracle:thin:@localhost:1521:compare";</div><div><span style="white-space:pre">			</span>for (count = 0; count &lt; 135; count++) {</div><div><span style="white-space:pre">				</span>conn[count] = DriverManager.getConnection(url,"nxcompare", "64compare");</div><div><span style="white-space:pre">				</span>stmt[count] = conn[count].createStatement();</div><div><span style="white-space:pre">				</span>rs[count] = stmt[count].executeQuery("SELECT * FROM users");</div><div><span style="white-space:pre">				</span>System.out.print(count + "\t");</div><div><span style="white-space:pre">			</span>}</div><div><span style="white-space:pre">		</span>} catch (SQLException ex1) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex1.toString());</div><div><span style="white-space:pre">		</span>} catch (InstantiationException ex2) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex2.toString());</div><div><span style="white-space:pre">		</span>} catch (ClassNotFoundException ex3) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex3.toString());</div><div><span style="white-space:pre">		</span>} catch (IllegalAccessException ex4) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex4.toString());</div><div><span style="white-space:pre">		</span>} finally {</div><div><span style="white-space:pre">			</span>try {</div><div><span style="white-space:pre">				</span>System.out.println("\n系统打开的连接数"+ count--+ " oracle connections.\n请按回车按钮，进行关闭");</div><div><span style="white-space:pre">				</span>System.in.read();</div><div><span style="white-space:pre">				</span>System.out.println("\n闭关连接:");</div><div><span style="white-space:pre">				</span>for (; count &gt;= 0; count--) {</div><div><span style="white-space:pre">					</span>rs[count].close();</div><div><span style="white-space:pre">					</span>stmt[count].close();</div><div><span style="white-space:pre">					</span>conn[count].close();</div><div><span style="white-space:pre">					</span>System.out.print(count + "\t");</div><div><span style="white-space:pre">				</span>}</div><div><span style="white-space:pre">			</span>} catch (SQLException ex) {</div><div><span style="white-space:pre">				</span>System.out.println("\n 关闭连接出现SQLException："+ ex.toString());</div><div><span style="white-space:pre">			</span>} catch (IOException io_ex) {</div><div><span style="white-space:pre">				</span>System.out.println("\n 关闭连接出现IOException："+ io_ex.toString());</div><div><span style="white-space:pre">			</span>}</div><div><span style="white-space:pre">		</span>}</div><div><span style="white-space:pre">	</span>}</div><div><span style="white-space:pre">	</span></div><div><span style="white-space:pre">	</span>/**</div><div><span style="white-space:pre">	</span> * 数据库连接方法（Mysql）</div><div><span style="white-space:pre">	</span> */</div><div><span style="white-space:pre">	</span>public void testMysqlMaxConn() {</div><div><span style="white-space:pre">		</span>try {</div><div><span style="white-space:pre">			</span>Class.forName("com.mysql.jdbc.Driver").newInstance();</div><div><span style="white-space:pre">			</span>String url = "jdbc:mysql://localhost:3306/tm?useUnicode=true&amp;characterEncoding=GBK";</div><div><span style="white-space:pre">			</span>for (count = 0; count &lt; 100; count++) {</div><div><span style="white-space:pre">				</span>conn[count] = DriverManager.getConnection(url,"root", "root");</div><div><span style="white-space:pre">				</span>stmt[count] = conn[count].createStatement();</div><div><span style="white-space:pre">				</span>rs[count] = stmt[count].executeQuery("SELECT * FROM userinfo");</div><div><span style="white-space:pre">				</span>System.out.print(count + "\t");</div><div><span style="white-space:pre">			</span>}</div><div><span style="white-space:pre">		</span>} catch (SQLException ex1) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex1.toString());</div><div><span style="white-space:pre">		</span>} catch (InstantiationException ex2) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex2.toString());</div><div><span style="white-space:pre">		</span>} catch (ClassNotFoundException ex3) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex3.toString());</div><div><span style="white-space:pre">		</span>} catch (IllegalAccessException ex4) {</div><div><span style="white-space:pre">			</span>System.out.println("\n" + ex4.toString());</div><div><span style="white-space:pre">		</span>} finally {</div><div><span style="white-space:pre">			</span>try {</div><div><span style="white-space:pre">				</span>System.out.println("\n系统打开的连接数"+ count--+ " MYSQL connections.\n请按回车按钮，进行关闭");</div><div><span style="white-space:pre">				</span>System.in.read();</div><div><span style="white-space:pre">				</span>System.out.println("\n闭关连接:");</div><div><span style="white-space:pre">				</span>for (; count &gt;= 0; count--) {</div><div><span style="white-space:pre">					</span>rs[count].close();</div><div><span style="white-space:pre">					</span>stmt[count].close();</div><div><span style="white-space:pre">					</span>conn[count].close();</div><div><span style="white-space:pre">					</span>System.out.print(count + "\t");</div><div><span style="white-space:pre">				</span>}</div><div><span style="white-space:pre">			</span>} catch (SQLException ex) {</div><div><span style="white-space:pre">				</span>System.out.println("\n 关闭连接出现SQLException："+ ex.toString());</div><div><span style="white-space:pre">			</span>} catch (IOException io_ex) {</div><div><span style="white-space:pre">				</span>System.out.println("\n 关闭连接出现IOException："+ io_ex.toString());</div><div><span style="white-space:pre">			</span>}</div><div><span style="white-space:pre">		</span>}</div><div><span style="white-space:pre">	</span>}</div><div></div><div><span style="white-space:pre">	</span>/**</div><div><span style="white-space:pre">	</span> * 测试方法</div><div><span style="white-space:pre">	</span> *&nbsp;</div><div><span style="white-space:pre">	</span> * @param args</div><div><span style="white-space:pre">	</span> * @throws Exception</div><div><span style="white-space:pre">	</span> */</div><div><span style="white-space:pre">	</span>public static void main(String args[]) throws Exception {</div><div>//<span style="white-space:pre">		</span>new testMaxConn().testOracleMaxConn(); //测试Oracle默认最大连接数135</div><div><span style="white-space:pre">		</span>new testMaxConn().testMysqlMaxConn(); //测试MYSQL，默认最大连接数100</div><div><span style="white-space:pre">	</span>}</div><div></div><div>}</div></div><img src ="http://www.blogjava.net/kxbin/aggbug/358775.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-09-16 11:00 <a href="http://www.blogjava.net/kxbin/articles/358775.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>mysql中文乱码解决方法</title><link>http://www.blogjava.net/kxbin/articles/358626.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 14 Sep 2011 08:30:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/358626.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/358626.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/358626.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/358626.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/358626.html</trackback:ping><description><![CDATA[<div><div>乱码问题解决</div><div>my.ini中的default-character-set = gb2312&nbsp;</div><div>然后 String url = "jdbc:mysql://localhost:3306/LibraryManagementSystem?useUnicode=true&amp;characterEncoding=GB2312";&nbsp;</div><div>在hibernate的乱码问题</div><div>&lt;property name="connection.url"&gt;</div><div>&nbsp; &nbsp;jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;amp;characterEncoding=UTF-8</div><div>&lt;/property&gt;</div></div><img src ="http://www.blogjava.net/kxbin/aggbug/358626.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-09-14 16:30 <a href="http://www.blogjava.net/kxbin/articles/358626.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>批处理执行创建表空间和表MYSQL</title><link>http://www.blogjava.net/kxbin/articles/358624.html</link><dc:creator>kxbin</dc:creator><author>kxbin</author><pubDate>Wed, 14 Sep 2011 08:25:00 GMT</pubDate><guid>http://www.blogjava.net/kxbin/articles/358624.html</guid><wfw:comment>http://www.blogjava.net/kxbin/comments/358624.html</wfw:comment><comments>http://www.blogjava.net/kxbin/articles/358624.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kxbin/comments/commentRss/358624.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kxbin/services/trackbacks/358624.html</trackback:ping><description><![CDATA[分两步：
<br />1。建立一个bat文件，内容如下： <br /><div><div>@echo off</div><div>"C:\Program Files\MySQL\MySQL Server 5.0\bin\mysql.exe" -h localhost --user=root --password=root --port=3306 mysql &lt; tm.sql &gt; d:/tm.log</div><div>echo 数据库和表创建成功，登录的初始用户名:admin；密码:admin;用户类型为：管理员</div><div>echo 请按任意键退出...........!</div><div>pause&gt;nul</div></div>2。建立一个sql文件，内容如下：&nbsp;<br /><div><div>-- 如果已经存在数据库，则删除</div><div>DROP database IF EXISTS `tm`;</div><div>create &nbsp; database &nbsp; tm &nbsp; default &nbsp; character &nbsp; set &nbsp; gbk;</div><div>USE tm;</div><div>set names gbk;&nbsp;</div><div>-- 1 用户账号管理 ==============================================================</div><div>create table userInfo&nbsp;</div><div>(</div><div>&nbsp; &nbsp;id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;not null,</div><div>&nbsp; &nbsp;userName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(20), -- 用户名</div><div>&nbsp; &nbsp;password &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(20), -- 密码</div><div>&nbsp; &nbsp;level &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;varchar(10), -- 级别</div><div>&nbsp; &nbsp;remark &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(100), -- 备注</div><div>&nbsp; &nbsp;createDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; datetime, &nbsp; -- 创建时间</div><div>&nbsp; &nbsp;deleteFlag &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varchar(1), -- 是否删除(Y删除,N正常)</div><div>&nbsp; &nbsp;constraint PK_USERINFO primary key clustered (id)</div><div>);</div><div>-- 默认添加一个用户</div><div>insert into userInfo values(1,'admin','admin','管理员','',now(),'N');</div></div><img src ="http://www.blogjava.net/kxbin/aggbug/358624.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kxbin/" target="_blank">kxbin</a> 2011-09-14 16:25 <a href="http://www.blogjava.net/kxbin/articles/358624.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>