mysql 共享锁 排他锁 防插入锁
试验1
事务1:
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test")
while str!="1":
str = raw_input()
cursor.execute("update test id=id-1")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
在mysql命令行中输入以下:
mysql> select * from test;
当事务1中等待输入1时,显示出select匹配的行
当事务1中输入1时,显示结果如下:
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
结论:当select时会检测当前是否有事务会修改(比影响要确切一些)当前的记录时,才会被阻塞。
试验2
在事务1:
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test lock in share mode")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
在mysql命令行中输入以下:
mysql> update test set id=id-1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
结论:当事务中包含select ...lock in share mode的时候,相关记录将会被锁住,不允许进行修改。
试验3
执行下面的python脚本两遍,并且同时输入1,2之后
#!/usr/bin/python
import time
import MySQLdb;
conn = MySQLdb.connect(host="localhost",port=3306,user="root",passwd="asdf",db="test",unix_socket="/data/mysql_3306/mysql.sock")
cursor = conn.cursor()
cursor.execute("select * from test lock in share mode")
str = ""
while str!="1":
str = raw_input()
while str!="2":
str = raw_input()
cursor.execute("update test set id=id-1")
while str!="exit":
str = raw_input()
cursor.close()
conn.close()
[root@TJSJHL196-139 tmp]# python test_transaction.py
1
2
Traceback (most recent call la
相关文档:
mysql> show variables like "autocommit";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
设置完之后看看该值
你要开启一个事务,直接发送begin 。。。。commit就可以,何必要去 ......
从A主机导入数据到B主机
mysqldump -hA -uusername -ppassword db_name >dumpfile
mysql -hB -uusername -ppassword
mysql>create database db_name2;
mysql>use db_name2;
mysql>source dumpfile;
先用dump把A中的数据库db_name导入到文件dumpfile,然后在B主机上,创建一个新的数据库db_name2,使用 ......
今天读到
When should you store serialized objects in the database?
,
其中针对
FriendFeed使用Mysql来存储
作为引题,
建议在考虑此方案时一定要三思。为什么?参考这里
Kiss KISS KISS
. 其实
Schema-Less没有错,但不能什么场景都上此方案,要
分析利弊,减少不必要的应用层复杂度。
文中提到的Serializ ......
1.普通log
记录所有sql操作,包括select,show语句。
设置
默认是不打开此log。
打开时可以指定目录,不指定时保存在数据库录目录下。
[mysqld]
log = /data/logs/mysql.log #此方式为指定文件
log = 1 ......
原文链接 http://database.51cto.com/art/200904/117957.htm
以下是涉及到插入表格的查询的5种改进方法:
1)使用LOAD DATA INFILE从文本下载数据这将比使用插入语句快20倍。
2)使用带有多个VALUES列表的INSERT语句一次插入几行这将比使用一个单行插入语句快几倍。调整bulk_insert_buffer_size变量也能提高(向包含行的表 ......