MySQL架构扩展是指通过增加硬件资源、优化软件配置或采用分布式架构等方式,提升MySQL数据库的性能、可用性和可扩展性。这通常涉及到对数据库进行垂直扩展(增加单个服务器的资源)或水平扩展(增加更多的服务器来分担负载)。
以下是一个简单的MySQL读写分离示例,使用Python和PyMySQL库:
import pymysql
# 写库配置
write_config = {
'host': 'write_host',
'user': 'user',
'password': 'password',
'database': 'db_name',
'charset': 'utf8mb4'
}
# 读库配置
read_config = {
'host': 'read_host',
'user': 'user',
'password': 'password',
'database': 'db_name',
'charset': 'utf8mb4'
}
def execute_write_query(query, params=None):
with pymysql.connect(**write_config) as conn:
with conn.cursor() as cursor:
cursor.execute(query, params)
conn.commit()
def execute_read_query(query, params=None):
with pymysql.connect(**read_config) as conn:
with conn.cursor() as cursor:
cursor.execute(query, params)
result = cursor.fetchall()
return result
# 示例:插入数据
execute_write_query("INSERT INTO table_name (column1, column2) VALUES (%s, %s)", ('value1', 'value2'))
# 示例:查询数据
result = execute_read_query("SELECT * FROM table_name WHERE column1 = %s", ('value1',))
print(result)
领取专属 10元无门槛券
手把手带您无忧上云