首页 > 数据库 >python管理MySQL数据库 mysql5.7读写分离 配置mycat(twenty-nine day)

python管理MySQL数据库 mysql5.7读写分离 配置mycat(twenty-nine day)

时间:2024-08-16 17:22:30浏览次数:14  
标签:python twenty mysql5.7 local usr mysql mysql57 mycat root

一、pymysql管理数据库

1、搭建主mysql5.7

[root@mysql57 ~]# ls
anaconda-ks.cfg  mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
[root@mysql57 ~]# tar -xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz 
[root@mysql57 ~]# ls
anaconda-ks.cfg
mysql-5.7.44-linux-glibc2.12-x86_64
mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz
[root@mysql57 ~]# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
[root@mysql57 ~]# rm -rf /etc/my.cnf
[root@mysql57 ~]# mkdir /usr/local/mysql/mysql-files
[root@mysql57 ~]# useradd -r -s /sbin/nologin mysql


[root@mysql57 ~]# chown mysql:mysql /usr/local/mysql/mysql-files/
[root@mysql57 ~]# ls /usr/local/mysql/
bin   docs     lib      man          README  support-files
data  include  LICENSE  mysql-files  share
[root@mysql57 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57
[root@mysql57 ~]# service mysql57 start
Starting MySQL.Logging to '/usr/local/mysql/data/mysql57.err'.
 SUCCESS! 

[root@mysql57 ~]# /usr/local/mysql/bin/mysql -p

mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)
 
mysql> create user 'lxe'@'%' identified by 'lxe';
Query OK, 0 rows affected (0.00 sec)
 
mysql> grant all on *.* to 'lxe'@'%';
Query OK, 0 rows affected (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql> create table user(id int primary key auto_increment,username varchar(45) not null,password varchar(45) not null); 
Query OK, 0 rows affected (0.00 sec)
 
mysql> insert into user (username,password)values("aaa","aaa");
Query OK, 1 row affected (0.00 sec)
 
mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | aaa      | aaa      |
+----+----------+----------+
1 row in set (0.00 sec)

[root@localhost ~]# python3 

>>> import pymysql
>>>conn=pymysql.connect(host="192.168.8.176",port=3306,database="test",user="lxe",password="lxe");
>>> cursor=conn.cursor()
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
>>> conn=pymysql.connect(host="192.168.8.176",port=3306,database="test",user="root",password="root");
>>> cursor.execute("create user 'slave0'@'%' identified by 'slave0'")  #创建用户
>>> cursor=conn.cursor()  
 
>>> cursor.execute("grant replication slave on *.* to 'slave0'@'%'")  #给权限
>>> cursor.execute("flush privileges")   #刷新
 
>>> cursor.execute("flush tables with read lock")
0
>>> cursor.execute("unlock tables")   #锁表
0
>>> cursor.execute("show master status")
0
>>> print(cursor.fetchall())
()

[root@mysql57 ~]# vim /usr/local/mysql/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
port=3306
log-error=/usr/local/mysql/data/db01-master.err
log-bin=/usr/local/mysql/data/binlog
server-id=10
character_set_server=utf8mb4

 [root@mysql57 ~]# service mysql57 restart

>>> conn=pymysql.connect(host="192.168.8.176",port=3306,database="test",user="lxe",password="lxe");
>>> cursor=conn.cursor()
>>> cursor.execute("show master status")
1
>>> print(cursor.fetchall())
(('binlog.000001', 154, '', '', ''),)
>>> cursor.execute("unlock tables")
0
2、从服务器

[root@salve57 ~]# tar -xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz 
[root@salve57 ~]# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
[root@salve57 ~]# ls /usr/local/mysql/
bin   include  LICENSE  README  support-files
docs  lib      man      share
[root@salve57 ~]# mkdir /usr/local/mysql/mysql-files
[root@salve57 ~]# useradd -r -s /sbin/nologin mysql
[root@salve57 ~]# chown mysql:mysql /usr/local/mysql/mysql-files/
[root@salve57 ~]# chmod 750 /usr/local/mysql/mysql-files/

[root@slave57 ~]# rm -rf /etc/my.cnf
[root@slave57 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql8

[root@salve57 ~]# yum -y install rsync

[root@salve57 ~]# vim /usr/local/mysql/data/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/err.log
log-relay=/usr/local/mysql/data/relaylog
character_set_server=utf8mb4
server-id=11

[root@salve57 ~]# sed -i '$aexport PATH=$PATH:/usr/local/mysql/bin' /etc/profile

[root@salve57 ~]# sed -n '$p' /etc/profile

export PATH=$PATH:/usr/local/mysql/bin
[root@salve57 ~]# source /etc/profile
[root@salve57 ~]# chkconfig --add mysql57
[root@salve57 ~]# chkconfig mysql57 on

[root@salve57 ~]# service mysql57 start

3、主服务器

[root@mysql57 ~]# chmod 750 /usr/local/mysql/mysql-files/
[root@mysql57 ~]# rm -rf /usr/local/mysql/data/auto.cnf 
[root@mysql57 ~]# yum -y install rsync
[root@mysql57 ~]# rsync -av /usr/local/mysql/data [email protected]:/usr/local/mysql/

二、企业架构MySQL读写分离

三、mycat读写分离

Mycat数据库中间件

国内最活跃的、性能最好的开源對据库中间件!

官方网址:http://www.mycat.io/https://github.com/MyCATApache/MyCAT-Server/

因为mycat是由java语言开发,必须使用java的运行环境才能进行启动和操作

步骤
1、上传jdk和mycat安装包

[root@mycat ~]# ls
jdk-8u192-linux-x64.tar.gz
Mycat-server-1.6.5-release-20180122220033-linux.tar.gz

2、解压并添加到指定的位置

[root@mycat ~]# tar -xf jdk-8u192-linux-x64.tar.gz 
[root@mycat ~]# tar -xf Mycat-server-1.6.5-release-20180122220033-linux.tar.gz 
[root@mycat ~]# cp -r jdk1.8.0_192/ /usr/local/jdk
[root@mycat ~]# cp -r mycat/ /usr/local/
[root@mycat ~]# ls /usr/local/jdk/

 3、配置jdk文件

[root@mycat ~]# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
[root@mycat ~]# sed -n '$p' /etc/profile
export JAVA_HOME=/usr/local/jdk
[root@mycat ~]# source /etc/profile
[root@mycat ~]# $JAVA_HOME
-bash: /usr/local/jdk: 是一个目录
[root@mycat ~]# sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin' /etc/profile
[root@mycat ~]# sed -n '$p' /etc/profile
export PATH=$PATH:$JAVA_HOME/bin
[root@mycat ~]# source /etc/profile
[root@mycat ~]# $PATH
-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/jdk/bin: 没有那个文件或目录
[root@mycat ~]# javac -version
javac 1.8.0_192

4、测试启动mycat 

[root@mycat ~]# /usr/local/mycat/bin/mycat console

 5、找到server.xml schema.xml文件并更改
[root@mycat ~]# vim /usr/local/mycat/conf/server.xml
 93         <user name="lxe" defaultAccount="true">
 94                 <property name="password">lxe</property>
 95                 <property name="schemas">test</property>
 
107 <!--
108         <user name="user">
109                 <property name="password">user</property>
110                 <property name="schemas">TESTDB</property>
111                 <property name="readOnly">true</property>
112         </user>
113 -->
 5         <schema name="test" dataNode="dn1" checkSQLschem    a="false" sqlMaxLimit="100">
  6         </schema>
  7         <dataNode name="dn1" dataHost="localhost1" datab    ase="test" />
  8 <!--    <dataNode name="dn2" dataHost="localhost1" datab    ase="db2" />
  9         <dataNode name="dn3" dataHost="localhost1" datab    ase="db3" /> -->
 
 14         <dataHost name="localhost1" maxCon="1000" minCon    ="10" balance="0"
 15                           writeType="0" dbType="mysql" d    bDriver="native" switchType="1"  slaveThreshold="100">
 16                 <heartbeat>select user()</heartbeat>
 17                 <!-- can have multi write hosts -->
 18                 <writeHost host="hostM1" url="192.168.8.    176:3306" user="lxe"
 19                                    password="lxe">
 20                         <!-- can have multi read hosts -    ->
 21                         <readHost host="hostS2" url="192    .168.8.177:3306" user="lxe" password="lxe" />
 22                 </writeHost>
 23 <!--            <writeHost host="hostS1" url="localhost:    3316" user="root"
 24                                    password="123456" />-
    ->
6、启动mycat并测试 

[root@mycat ~]# /usr/local/mycat/bin/mycat start
[root@mycat ~]# netstat -lnput | grep 8066

[root@client ~]# cd mysql-5.7.44-linux-glibc2.12-x86_64/
[root@client mysql-5.7.44-linux-glibc2.12-x86_64]# cd bin/
[root@client bin]# ./mysql -h192.168.8.178 -P8066 -ulxe -plxe

mysql> show databases;
+--------------------+
| Database           |
+--------------------+              |
| test               |
+--------------------+
 
 
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | aaa      | aaa      |
+----+----------+----------+
1 row in set (0.00 sec)
 
mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 10    |
+---------------+-------+
1 row in set (0.02 sec)

标签:python,twenty,mysql5.7,local,usr,mysql,mysql57,mycat,root
From: https://blog.csdn.net/m0_75005437/article/details/141241418

相关文章

  • Python - Structural Design Patterns
    •TheadapterpatternTheadapterpatternisastructuraldesignpatternthathelpsusmaketwoincompatibleinterfaces compatible.Whatdoesthatreallymean?Ifwehaveanoldcomponentandwewanttouseitinanew system,oranewcomponentthatwew......
  • python的基础知识入门
    一.初聊Python为什么要学习Python?在学习Python之前,你不要担心自己没基础或“脑子笨”,我始终认为,只要你想学并为之努力,就能学好,就能用Python去做很多事情。在这个喧嚣的时代,很多技术或概念会不断兴起,我希望你能沉下心来去学习,不要急于求成,一步一个脚印。当你把某个技术学好、......
  • QT设置回调函数给python调用——参数法
    这种方法将回调函数作为python函数参数对象的方法来使用。Qt已经添加了Python库,并且能够正常调用Python的API,可以成功调用Python的代码块,这部分可以参考我另外一篇博客:1.QT相关函数定义1.1创建回调函数例如下面两个函数//实际的回调函数voidprintValue(intvalue){......
  • QT设置回调函数给python调用——内置模块法
    1.QT相关函数定义和 QT设置回调函数给python调用——参数法中的定义相同如下://实际的回调函数voidprintValue(intvalue){qDebug()<<"printValuevalue:"<<value;}intgetValue(intvalue){qDebug()<<"getValuevalue:"<<value;......
  • Python借助Selenium,保留原样式拷贝网站资源
    importurllib3fromseleniumimportwebdriverfrombs4importBeautifulSoupimportosimportrequestsfromurllib.parseimporturljoin,urlparseimportbase64importrefromrequests.adaptersimportHTTPAdapterfromrequests.packages.urllib3.util.retryim......
  • Python魔法应用:自动化日常任务:让Python成为你的私人助理
    自动化日常任务:让Python成为你的私人助理引言自动化文本处理:批量重命名文件、整理数据批量重命名文件整理数据自动化邮件发送:使用smtplib发送邮件自动化网页交互:使用requests库获取网页数据结语引言在日常工作和学习中,我们常常会遇到大量重复且繁琐的任务,这些任务......
  • python 之 pytest框架
    一、pytest单元测试框架1、什么是单元测试,针对软件的最小单位(函数,方法,接口)进行正确性的检查测试。 2、单元测试框架(按语言分)java:junit和testingpython:unittest和pytest 3、单元测试主要是做什么呢?(1)、测试发现:从多个文件里面去找到测试用例(2)、测试执行:按......
  • python蟒蛇绘制
    turtle海龟绘图setup()设置窗体大小及位置setup()不是必须的importturtleturtle.setup(650,350,200,200)turtle.penup()turtle.fd(-250)turtle.pendown()turtle.pensize(25)turtle.pencolor("purple")turtle.seth(-40)foriinrange(4):turtle.circle(40,80)......
  • Python温度的转换
    神清气爽哈哈哈程序代码:#温度的转换TempStr=input("请输入带有符号的温度值:")ifTempStr[-1]in['F','f']:#判断字符转最后一个字符是否为F或fc=(eval(TempStr[0:-1])-32)/1.8#将摄氏度转化为华氏度print("转换后的温度是{:.2f}C".format(c))#eva......
  • 【Python SHA256 摘要算法】
    SHA256是一种广泛使用的密码散列函数,用于生成数据的唯一指纹,即散列值。它属于SHA-2家族,该家族还包括SHA-384和SHA-512算法。SHA256算法在许多领域都有应用,例如:数据完整性验证:用于验证数据在传输或存储过程中是否被篡改。例如,在下载软件时,通常会提供软件的SHA256哈......