使用python3搭建Linux-mariadb主从架构
环境准备两台:
192.168.193.90 master
192.168.193.91 slave
需要Linux装python环境:
https://www.cnblogs.com/kingzhe/p/11124527.html
在做主从时,要保证两个数据库的信息一致
[root@node1 ~]# mysql_secure_installation [root@node2 ~]# mysql_secure_installation
master
import configparser
import os
def config_mariadb_yum():
exists = os.path.exists('/etc/yum.repos.d/mariadb.repo')
if exists:
print('mariadb.repo exist')
yum_install_mariadb()
else:
config = configparser.ConfigParser()
config.read('/etc/yum.repos.d/mariadb.repo', encoding='utf-8')
config.add_section('mariadb')
config.set('mariadb', 'name', 'MariaDB')
config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
config.set('mariadb', 'gpgcheck', '')
config.write(open("/etc/yum.repos.d/mariadb.repo", "w"))
yum_install_mariadb() def yum_install_mariadb():
res1 = os.system('yum install MariaDB -y > /dev/null 2&>1')
if res1 == :
config = configparser.ConfigParser()
config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8')
config.set('server','server_id','')
config.set('server','log-bin','mysql-bin')
config.write(open("/etc/my.cnf.d/server.cnf", "w"))
res2 = os.system('service mariadb restart')
if res2 == :
os.system('''mysql -uroot -proot -e "grant replication slave on *.* to 'slave'@'%' identified by 'slave'"''')
os.system("mysql -uroot -proot -e 'show master status'") def main():
config_mariadb_yum()
if __name__ == '__main__':
main()
slave
import configparser
import os
master_ip = input('master_ip:').strip()
log_file = input('log_file:').strip()
pos = input('pos:').strip()
def config_mariadb_yum():
exists = os.path.exists('/etc/yum.repos.d/mariadb.repo')
if exists:
print('mariadb.repo exist')
yum_install_mariadb()
else:
config = configparser.ConfigParser()
config.read('/etc/yum.repos.d/mariadb.repo', encoding='utf-8')
config.add_section('mariadb')
config.set('mariadb', 'name', 'MariaDB')
config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
config.set('mariadb', 'gpgcheck', '')
config.write(open("/etc/yum.repos.d/mariadb.repo", "w"))
yum_install_mariadb() def yum_install_mariadb():
res1 = os.system('yum install MariaDB -y > /dev/null 2&>1')
if res1 == :
config = configparser.ConfigParser()
config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8')
config.set('server','server_id','')
config.write(open("/etc/my.cnf.d/server.cnf", "w"))
res2 = os.system('service mariadb restart')
if res2 == :
os.system('''mysql -uroot -proot -e "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s"''' % (master_ip,log_file,pos))
os.system("mysql -uroot -proot -e 'start slave;'") def main():
config_mariadb_yum()
if __name__ == '__main__':
main()
在pycharm写入 rz上传
[root@node1 ~]# python3 1.py
[root@node1 ~]# python3 .py
mariadb.repo hhh
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin. | | | |
+------------------+----------+--------------+------------------+
master成功
在运行是遇到的报错
[root@node1 ~]# python .py
Traceback (most recent call last):
File "1.py", line , in <module>
import configparser
ImportError: No module named configparser #这条报错是没有configparser模块
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# pip install configparser #下载模块
Collecting configparser
Downloading https://files.pythonhosted.org/packages/ba/05/6c96328e92e625fc31445d24d75a2c92ef9ba34fc5b037fe69693c362a0d/configparser-3.7.4-py2.py3-none-any.whl
Installing collected packages: configparser
Successfully installed configparser-3.7.4 #下载成功
rz上传slave节点
[root@node2 ~]# python3 2.py
[root@node2 ~]# python3 .py
master_ip:192.168.193.90
log_file:mysql-bin.
pos:
Redirecting to /bin/systemctl restart mariadb.service
这样就完成了主从架构
使用python3搭建Linux-mariadb主从架构的更多相关文章
- 使用python3脚本部署mariadb主从架构
环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. master import paramikossh=paramiko ...
- mariadb主从架构
mariadb主从架构(异步)和集群 一般应用的场所是网站,主的机器是可以写可以读,从的机器可以读,也可以写,但不会同步.只有主的机器增删改,从的机器才会同步. 主从至少三个线程:dump.I/O t ...
- 使用Innobackupex快速搭建(修复)MySQL主从架构
MySQL的主从搭建大家有很多种方式,传统的mysqldump方式是很多人的选择之一.但对于较大的数据库则该方式并非理想的选择.使用Xtrabackup可以快速轻松的构建或修复mysql主从架构.本文 ...
- 使用python脚本部署mariadb主从架构
环境准备 一个脚本自动部署master服务 另一个部署slave服务 关闭主从节点的防火墙 以及事先设置好root远程登陆的权限. grant all on *.* to root@'%' iden ...
- python部署mariadb主从架构
主机部署: import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.repo ...
- python3自动部署mariadb主从
master import configparser import os def config_mariadb_yum(): exists = os.path.exists('/etc/yum.rep ...
- Redis 主从架构搭建
引言 准备搭建的是主从架构( Master/Slave )中的一主两从模式:其中 Master 为 Redis 的主服务器,主要负责写操作,两个 Slave 为 Redis 的从服务器,主要负责读操作 ...
- Mariadb 主从
一 mariadb主从多用于网站架构,因为该主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构 ...
- redis主从架构的搭建
本项目采用主从架构,一主两从一个哨兵.在x.x.x.69上部署主节点,在70上部署从节点1和哨兵节点,在71上部署从节点2. 准备: 1.首先上传redis文件到三台linux上,目录/home/sy ...
随机推荐
- CF1081G Mergesort Strikes Back
题目大意: 给定\(n\),\(k\),\(mod\),求随机排列在\(k\)层归并排序下逆序对的期望. 题解 考虑这\(k\)层归并会把序列分成若干个块. 这些块内的顺序是原序列的相对顺序,我们要把 ...
- Gradle 编译加速
参考:http://www.jianshu.com/p/200d55b4d40a http://blog.isming.me/2015/03/18/android-build-speed-up/ ht ...
- CSS - 层叠上下文(The stacking context)
对 MDN 的上的例子的拓展 Root - DIV #1(z-index: 5) - DIV #2(z-index: 2) - DIV #3(z-index: 4) - DIV #4(z-index: ...
- DataFrame 结构
概念 DataFrame 是表格型的数据结构 ,DataFrame 本质上可以看做是由series 组成的字典, 它既有行索引,也有列索引. 它并不是列表,也不是字典,.
- Binder的Native实现libbinder
libbinder – Binder的Native实现 出于性能和代码统一性的角度考虑,Binder IPC并不Java和Native环境里各实现一次,而只是分别在不同的执行环境里提供使用的接口.使用 ...
- TNS-01106: Listener using listener name LISTENER has already been started
-- 启动监听,提示已经启动. [oracle@sh ~]$ lsnrctl startLSNRCTL for Linux: Version 12.1.0.2.0 - Production on 06 ...
- SpringBoot使用webservice
Pom.xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- GCD and LCM HDU 4497 数论
GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...
- SCAU 2015 GDCPC team_training1
A: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1525 题意:前两段都是废话 , 然后给出一个 p( p(a) ) = p(a) 的公式给你 ...
- A.Gennady and a Card Game
http://m3.codeforces.com/contest/1097/problem/A Gennady and a Card Game time limit per test 1 second ...