5月 27

python连接oracle和mysql备忘

import cx_Oracle
    
def sql_comm(sql_run):
    db = cx_Oracle.connect(‘user’, ‘passwd’, ‘IP:port/sid’) 
    try:
        cursor=db.cursor()
    except cx_Oracle.ERROR,e:
        print “Error %d:%s”%(e.args[0],e.args[1])
    try:
           cursor.execute(sql_run)
    result_set=cursor.fetchall()
           cursor.close()
           db.commit()
           db.close()
    return result_set
    except e:
        print “Error %s”%(e.args[0])
        cursor.close()
        db.close()

python连接mysql

import MySQLdb

def sql_comm(sql_run):
        try:
                conn=MySQLdb.connect(host=host,user=username,passwd=pwd,db=database)
                cursor = conn.cursor()
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
        try:
                cursor.execute(sql_run)
                result_set=cursor.fetchall()
                cursor.close()
                db.close()
                return result_set
        except MySQLdb.Error,e:
                print “Error %d:%s”%(e.args[0],e.args[1])
                cursor.close()
                db.close()

注意部分,在mysql修改数据时最好使用commit,如果只是select就不用了,不然得不到数据。
在oracle和mysql插入大量数据时,可以使用executemany,需要注意的是,oracle插入大量
数据时要将数据类型为元组,然后放到列表中。建议一次插入10000行。

8月 19

MySql日常操作记录

[整理日:2010-08-19]

1)使用innodb引擎独立表空间,在配置文件my.cnf中添加如下,重启数据库。
[mysqld]
innodb_file_per_table

2)Drop table操作自动回收表空间,如果对于统计分析或是日值表,删除大量数据后可以通过:
alter table TableName engine=innodb

OPTIMIZE TABLE TableName;

3)改变现有的表使用的存储引擎,用以下语句:
ALTER TABLE mytable ENGINE = MyISAM

4)设置InnoDB为默认引擎:在配置文件my.cnf中的 [mysqld] 下面加入default-storage-engine=INNODB 一句,保存。重启mysql服务器

5)查看当前进程和执行sql

show processlist ;

6) 清除30天前的 binlog
PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL 30 DAY);

7)自动清理30天前的binlog

在配置文件中添加expire_logs_days = 30

4月 09

忘记 mysql 密码

1. 先结束mysql 进程

# killall mysqld

2. 用mysql 安全模式运行并跳过权限难

# mysqld_safe –skip-grant-tables

3. 用root 登录 ,此时不需要密码

# mysql -u root

4. 现在开始修改密码了

mysql> use mysql;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> update user set Password = PASSWORD(‘your new password’) where User = ‘root’ ;

Query OK, 2 rows affected (0.02 sec)

Rows matched: 2 Changed: 2 Warnings: 0

mysql> flush privileges;

mysql> quit

Bye