用python3搭建Linux-mariadb主从架构
环境配置:
1.准备2台服务器,IP地址设置如下:
192.168.146.10 master
192.168.146.20 slave
2.需要Linux装python环境:
https://www.cnblogs.com/kingzhe/p/11124527.html
在做主从时,要保证两个数据库的信息一致
[root@localhost ~]# mysql_secure_installation [root@localhost ~]# 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', '1') 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 == 0: config = configparser.ConfigParser() config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8') config.set('server','server_id','1') 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 == 0: 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', '1') 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 == 0: config = configparser.ConfigParser() config.read('/etc/my.cnf.d/server.cnf', encoding='utf-8') config.set('server','server_id','2') config.write(open("/etc/my.cnf.d/server.cnf", "w")) res2 = os.system('service mariadb restart') if res2 == 0: 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()
传入到pychar中
[root@localhost ~]# python3 1.py
[root@localhost ~]# python3 1.py mariadb.repo hhh +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000006 | 630 | | | +------------------+----------+--------------+------------------+
master成功
在运行是遇到的报错
[root@localhost ~]# python 1.py Traceback (most recent call last): File "1.py", line 1, in <module> import configparser ImportError: No module named configparser #这条报错是没有configparser模块 [root@localhost ~]# 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@localhost ~]# python3 2.py
[root@localhost ~]# python3 2.py master_ip:192.168.146.10 log_file:mysql-bin.000006 pos:530 Redirecting to /bin/systemctl restart mariadb.service
完成架构配置
人能够凝炼成一颗石子,潜伏见底,让时光像 水一般在身上湍急而过,自己只知身在水中,不觉水流。