发布网友 发布时间:2022-04-07 20:08
共3个回答
懂视网 时间:2022-04-08 00:30
yosemite系统用brew
安装mysql可以参考: http://segmentfault.com/a/1190000000438233
安装mysqldb: pip install mysql-python
import MySQLdb as mdb import sys try: con = mdb.connect(‘localhost‘, ‘root‘, ‘password‘, ‘dbname‘) cur = con.cursor() cur.execute("select version()") ver = cur.fetchone() except mdb.Error, e: print "error %d: %s" % (e.args[0], e.args[1]) sys.exit(1) finally: if con: con.close()
数据库的操作很容易出现异常,最好用exception来处理,使用完后关闭连接con.close()
使用with可以简化代码,而且提供异常处理
con = mdb.connect(...) with con: cur = con.cursor() cur.execute("DROP TABLE IF EXISTS Writers") cur.execute("CREATE TABLE Writers(Id INT PRIMARY KEY AUTO_INCREMENT, Name VARCHAR(25))") cur.execute("INSERT INTO Writers(Name) VALUES(‘Jack London‘)") cur.execute("INSERT INTO Writers(Name) VALUES(‘Honore de Balzac‘)") cur.execute("INSERT INTO Writers(Name) VALUES(‘Lion Feuchtwanger‘)") cur.execute("INSERT INTO Writers(Name) VALUES(‘Emile Zola‘)") cur.execute("INSERT INTO Writers(Name) VALUES(‘Truman Capote‘)")
with con: cur = con.cursor() cur.execute("select * from Writers") rows = cur.fetchall() for row in rows: print row with con: cur = con.cursor() cur.execute("select * from Writers") for i in range(cur.rowcount): row = cur.fetchone() print row[0], row[1]
上面是两种查询的方式,一种fetchall()获取整个表格,一种每次fetchone(), cur.fetchall()返回的rows是一个tuple,并且每个元素row也是一个tuple,里面有表格的一行数据
如果希望以字典的形式返回结果,可以用dict cursor
con = mdb.connect(...) with con: cur = con.cursor(mdb.cursors.DictCursor) cur.execute("select * from Writers") rows = cur.fetchall() for row in rows: print row["id"], row["name"]
with con: cur = con.cursor() cur.execute("select * from Writers") desc = cur.description print "%s %3s" % (desc[0][0], desc[1][0])
输出((‘Id‘, 3, 1, 11, 11, 0, 0), (‘Name‘, 253, 17, 75, 75, 0, 1))
print desc[0][0], desc[1][0]就可以得到列名
mysql提供了插入图片数据的方法,图片是二进制数据,mysql中提供一种特殊的数据类型来存放二进制数据,BLOB
(binary large obj),包括TINYBLOB
,BLOB
,MEDIUMBLOB
,LONGBLOB
def read_images(): fin = open("togglebg.png") img = fin.read() return img with con: cur = con.cursor() cur.execute("create table images(id int primary key, data mediumblob)") data = read_images() cur.execute("insert into images values(1, %s)", (data, ))
def read_images(): fin = open("togglebg.png") img = fin.read() return img def writeImages(data): fout = open(‘hello.jpg‘, ‘wb‘) with fout: fout.write(data) with con: cur = con.cursor() cur.execute("select data from images where id = 1") data = cur.fetchone()[0] writeImages(data)
参考:http://zetcode.com/db/mysqlpython/
MySQLdb使用
标签:
热心网友 时间:2022-04-07 21:38
Asynchronous Replication Automatic failover
其原理是在一条异步复制通道上配置多个可用复制源,当某个复制源不可用时(宕机、复制链路中断),且 slave 的 IO 线程尝试重连无效,自动根据权重选择新的源继续同步。
准备一个 MGR 集群和单实例,模拟复制链路切换,当 primary 故障,slave 自动切换到其他节点。dbdeployer deploy replication --topology=group 8.0.22 --single-primarydbdeployer deploy single 8.0.22
2. 在从机上建立指向 MGR 主节点的复制通道,
change master to master_user='msandbox',master_password='msandbox', master_host='127.0.0.1',master_auto_position=1,source_connection_auto_failover=1,master_port=23223,master_retry_count=6,master_connect_retry=10 for channel 'mgr-single';
在 master_retry_count 和 master_connect_retry 的设置上要考虑尝试重连多久才切换复制源。
3. 在从机上配置 asynchronous connection auto failover
配置 asynchronous connection auto failover 的两个函数:
asynchronous_connection_failover_add_source(channel-name,host,port,network-namespace,weight)
asynchronous_connection_failover_delete_source(channel-name,host,port,network-namespace)
权重值大的被优先级选择,可以配合MGR的选举权重配置 asynchronous_connection_failover 的权重。当 MGR 节点切换,异步复制也能切换到新的主节点。
SELECT asynchronous_connection_failover_add_source('mgr-single','127.0.0.1',23223,null,100); SELECT asynchronous_connection_failover_add_source('mgr-single','127.0.0.1',23224,null,80); SELECT asynchronous_connection_failover_add_source('mgr-single','127.0.0.1',23225,null,50);start slave for channel 'mgr-single';
4. 检查异步复制通道是否启用 failover。
mysql> SELECT CHANNEL_NAME, SOURCE_CONNECTION_AUTO_FAILOVER FROM performance_schema.replication_connection_configuration; +--------------+---------------------------------+| CHANNEL_NAME | SOURCE_CONNECTION_AUTO_FAILOVER |+--------------+---------------------------------+| mgr-single | 1 |+--------------+---------------------------------+1 row in set (0.01 sec
5. 把 MGR 的 primary 节点 kill 掉,这个从节点会在尝试几轮重连失败后自动切换到次权重的复制源,其日志中会输出切换信息。
注意:当主节点故障,一旦复制链路成功 failover 后,在新的复制链路没有故障时,如果原主节点恢复,是不会回切的。如果当前复制链路发生故障,会再次选择权重高的进行切换
热心网友 时间:2022-04-07 22:56
连接数据库的代码如下