2024.8.15(python管理mysql、Mycat实现读写分离)

一、python管理mysql

1、搭建主mysql

[root@mysql57 ~]# tar -xf 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 ~]# chown 750 /usr/local/mysql/mysql-files/
[root@mysql57 ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql
[root@mysql57 ~]# ls /usr/local/mysql/
[root@mysql57 ~]# cp /usr/local/mysql/support-files/mysql.server
 /etc/init.d/mysql57
[root@mysql57 ~]# service mysql57 start
[root@mysql57 ~]# /usr/local/mysql/bin/mysql -p
Enter password: 

mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.00 sec)mysql> create user 'blt'@'%' identified by 'blt';
Query OK, 0 rows affected (0.00 sec)mysql> grant all on *.* to 'blt'@'%';
Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> create database if not exists test charset utf8mb4;
Query OK, 1 row affected (0.00 sec)mysql> use test;
Database changed
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.01 sec)mysql> insert into user (username,password)values("aaa","aaa");
Query OK, 1 row affected (0.02 sec)mysql> select * from user;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
|  1 | aaa      | aaa      |
+----+----------+----------+
1 row in set (0.00 sec)
2、pymysql

[root@client ~]# python3 

>>> import pymysql
>>>conn=pymysql.connect(host="192.168.8.150",port=3306,database="test",user="blt",password="blt");
>>> cursor=conn.cursor()
3、修改root权限
mysql> update mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
>>>conn=pymysql.connect(host="192.168.8.150",port=3306,database="test",user="root",password="root");
>>> curson=conn.cursor()
>>> cursor.execute("create user 'slave0'@'%' identified by 'slave0'")
0
>>> 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("flush tables with read lock")
0
>>> cursor.execute("show master status")
0
>>> print(cursor.fetchall())
()
>>> cursor.execute("show master status")
4、my.cnf

[root@mysql57 ~]# rm -rf /etc/my.cnf

[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.150",port=3306,database="test",user="blt",password="blt");
>>> cursor=conn.cursor()
>>> cursor.execute("show master status")
1
>>> print(cursor.fetchall())
(('binlog.000001', 154, '', '', ''),)
>>> cursor.execute("unlock tables")
0
5、slave数据库

[root@slave57 ~]# tar -xf mysql-5.7.44-linux-glibc2.12-x86_64.tar.gz 
[root@slave57 ~]# cp -r mysql-5.7.44-linux-glibc2.12-x86_64 /usr/local/mysql
[root@slave57 ~]# ls /usr/local/mysql/
[root@slave57 ~]# mkdir /usr/local/mysql/mysql-files
[root@slave57 ~]# useradd -r -s /sbin/nologin mysql
[root@slave57 ~]# chown mysql:mysql /usr/local/mysql/mysql-files/
[root@slave57 ~]# chmod 750 /usr/local/mysql/mysql-files/

6、主从同步

[root@mysql57 ~]# rm -rf /usr/local/mysql/data/auto.cnf 
[root@mysql57 ~]# yum -y install rsync

[root@mysql57 ~]# rsync -av /usr/local/mysql/data root@192.168.8.151:/usr/local/mysql/

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

[root@slave57 ~]# vim /usr/local/mysql/my.cnf
[root@slave57 ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql57
[root@slave57 ~]# sed -i '$aexport PATH=$PATH:/usr/local/mysql/bin' /etc/profile
[root@slave57 ~]# sed -n '$p' /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
[root@slave57 ~]# source /etc/profile
[root@slave57 ~]# chkconfig --add mysql57
[root@slave57 ~]# chkconfig mysql57
[root@slave57 ~]# chkconfig mysql57 on

7、错误

[root@slave57 ~]# service mysql57 start
Starting MySQL.2024-08-15T06:29:37.027747Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/usr/local/mysql/data/slave57.pid).

[root@slave57 ~]# mkdir /var/log/mariadb
[root@slave57 ~]# touch /var/log/mariadb/mariadb.log
[root@slave57 ~]# chown -R mysql:mysql /var/log/mariadb/
[root@slave57 ~]# service mysql57 start
Starting MySQL. SUCCESS! 

二、MyCat实现读写分离

1、添加新的虚拟主机,关闭防火墙

[root@mycat ~]# systemctl stop firewalld
[root@mycat ~]# setenforce 0

2、上传jdk和Mycat

[root@mycat ~]# ls

3、解压并上传到指定位置

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

4、查看并且配置jdk文件

[root@mycat ~]# ls /usr/local/jdk/

[root@mycat ~]# sed -i '$aexport JAVA_HOME=/usr/local/jdk' /etc/profile
[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 ~]# source /etc/profile

[root@mycat ~]# $PATH
-bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/jdk:/usr/local/jdk/bin: 没有那个文件或目录

[root@mycat ~]# javac -version
javac 1.8.0_192

5、测试启动Mycat

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

6、配置读写分离

[root@mycat logs]# vim /usr/local/mycat/conf/server.xml 

93         <user name="blt" defaultAccount="true">
94                 <property name="password">blt</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 -->

[root@mycat logs]# vim /usr/local/mycat/conf/schema.xml  

4         <!-- 1.名称为真实数据库名称,添加一个dataNode-->
5         <schema name="test" dataNode="dn1" checkSQLschema="false" sqlMaxLimit="100">
6         </schema>
7         <!-- <dataNode name="dn1$0-743" dataHost="localhost1" database="db$0-743"
8                 /> -->
9         <dataNode name="dn1" dataHost="localhost1" database="test" />
20   <writeHost host="hostM1" url="192.168.8.150:3306" user="blt" password="blt">
21            <!-- can have multi read hosts -->
22 <readHost host="hostS2" url="192.168.8. 151:3310" user="blt" password="blt" />
23                 </writeHost>
7、测试

[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.152 -P8066 -ublt -pblt

mysql> show databases;
+--------------------+
| Database           |
+--------------------+              |
| test               |
+--------------------+
1 rows in set (0.00 sec)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 -ADatabase 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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/403894.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qt-13 进度条(模态和非模态)

进度条-模态和非模态 progressdlg.hprogressdlg.cppmain.cpp运行图模态非模态 progressdlg.h #ifndef PROGRESSDLG_H #define PROGRESSDLG_H#include <QDialog> #include <QLabel> #include <QLineEdit> #include <QProgressBar> #include <QCombo…

[书生大模型实战营][L0][Task2] Python 开发前置知识

0. 任务&#xff1a;在 InternStudio 环境中实现功能&#xff1a; python 实现 wordcount函数&#xff0c;统计英文字符串单词的使用频率&#xff0c;并返回字典&#xff1b;vscode 远程调试 InternStudio 中的 python 代码 1. wordcount 函数实现 string.punctuation 是一个…

一键切换全球优质Linux 系统软件源及 Docker 源,轻松安装 Docker —— 适配广泛、零门槛、超强功能的开源脚本!

概述 linuxMirrors开源脚本为 GNU/Linux 系统用户提供了强大的工具,帮助用户轻松更换系统软件源并安装 Docker。脚本适配了多种国内外镜像站,经过测试具备良好的下载速度和 IPv6 兼容性,并且还包括了中国大陆教育网镜像站的选项。无需技术背景,文档提供了详尽的操作指引和常…

机器学习(3)-- 一元线性回归

文章目录 线性回归训练模型测试模型线性回归方程测试实用性 总结 线性回归 线性回归算法是一种用于预测一个或多个自变量&#xff08;解释变量&#xff09;与因变量&#xff08;响应变量&#xff09;之间关系的统计方法。这种方法基于线性假设&#xff0c;即因变量是自变量的线…

【网络安全】重置密码token泄露,实现账户接管

未经许可&#xff0c;不得转载。 文章目录 正文 正文 对某站点测试过程中&#xff0c;登录账户触发忘记密码功能点&#xff0c;其接口、请求及响应如下&#xff1a; PUT /api/v1/people/forgot_password 可以看到&#xff0c;重置密码token和密码哈希均在响应中泄露。 删除co…

【C#】虚部与实部

实数是数学中的一个基本概念&#xff0c;它包括了所有的有理数和无理数。实数集合是连续的&#xff0c;可以表示为数轴上的每一个点。 复数是实数的扩展&#xff0c;它允许进行除零以外的所有基本算术运算。复数由两部分组成&#xff1a;实部和虚部。 实部&#xff08;Real P…

【MySQL 07】表的增删查改 (带思维导图)

文章目录 &#x1f308; 一、insert 添加数据⭐ 1. 单行数据 全列插入⭐ 2. 多行数据 指定列插入⭐ 3. 插入否则更新⭐4. 插入否则替换 &#x1f308; 二、select 查询数据⭐ 1. select 列&#x1f319; 1.1 全列查询&#x1f319; 1.2 指定列查询&#x1f319; 1.3 查询字段…

Prettier+Vscode setting提高前端开发效率

文章目录 前言Prettier第一步&#xff1a;下载依赖&#xff08;团队合作&#xff09;或下载插件&#xff08;独立开发&#xff09;第二步&#xff1a;添加.prettierrc.json文件**以下是我使用的****配置规则** 第三步&#xff1a;添加.prettierignore文件**以下是我常用的****配…

OpenCV图像滤波(20)模糊处理函数stackBlur()的使用

操作系统&#xff1a;ubuntu22.04 OpenCV版本&#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言&#xff1a;C11 算法描述 stackBlur() 函数用于对图像进行模糊处理。该函数对图像应用了 stackBlur 技术。stackBlur 可以生成与高斯模糊相似的结果&#xff0c;而且随着模…

C++ 左值引用与右值引用超详解

目录 一 左值与右值 1.左值 2.右值 3.总结 二 左值引用与右值引用 1.左值引用 2.右值引用 3.总结与探究 3.1右值引用可以修改么&#xff1f;取地址么&#xff1f; 3.2左值引用与右值引用转化 左值引用 引用 右值 右值引用 引用 左值 3.3左值引用与右值引用相同之处 3.4左值…

栈与队列 - 逆波兰表达式求值

150. 逆波兰表达式求值 方法一&#xff1a;栈 /*** param {string[]} tokens* return {number}*/ var evalRPN function(tokens) {const stack [];for (const token of tokens) {if (isNaN(Number(token))) { // 非数字const n2 stack.pop(); // 出栈两个数字const n1 s…

【Unity开发】几种空值判断的性能测试

【Unity开发】几种空值判断的性能测试&#xff09; 项目优化过程中&#xff0c;一个非常细节的优化&#xff0c;就是在项目数据处理过程中&#xff0c;会用大量的null和“”空值的判断&#xff0c;参考了一些网友说的性能差别很大&#xff0c;是不是真的需要优化的问题&#xf…

16:【stm32】I2C的使用一:I2C片上外设的使用

I2C 1、片上外设1.1&#xff1a;寄存器与内部结构 2、通过I2C向外发送数据2.1&#xff1a;I2C的初始化2.1.1&#xff1a;初始化SCL和SDA2.1.2&#xff1a;使能时钟PCLK1&#xff08;APB1&#xff09;2.1.3&#xff1a;配置I2C1的参数 2.2&#xff1a;发送数据2.2.1&#xff1a;…

Ⅰ、基于 WebGPU 从 0 到 1 渲染 GLTF:第一个三角形

Ⅰ、基于 WebGPU 从 0 到 1 渲染 GLTF&#xff1a;第一个三角形 WebGPU 是一种面相网页的现代图形 API&#xff0c;由主要浏览器供应商开发。与 WebGL 相比&#xff0c;WebGPU 对 GPU 提供了更直接的控制&#xff0c;使应用程序能更有效地利用硬件&#xff0c;类似于 Vulkan 和…

如何在C++ QT 程序中集成cef3浏览器组件去显示网页?

目录 1、问题描述 2、为什么选择cef3浏览器组件 3、cef3组件的介绍与下载 4、将cef3组件封装成sdk 5、如何使用cef3组件加载web页面 5.1、了解CefApp与CefClient 5.2、初始化与消息循环 5.3、如何创建浏览器 5.4、重载CefClient类 6、在qt客户端集成cef组件 7、最后…

「12月·长沙」第三届传感、测量、通信和物联网技术国际会议(SMC-IoT 2024)

第三届传感、测量、通信和物联网技术国际会议&#xff08;SMC-IoT 2024&#xff09;将于2024年11月29日-2024年12月1日召开&#xff0c;由湖南涉外经济学院主办。会议中发表的文章将会被收录, 并于见刊后提交EI核心索引。 会议旨在围绕传感、测量、通信和物联网技术等相关研究…

基于node.js的宠物寄存管理系统,基于express的宠物寄存系统

摘 要 伴随着社会以及科学技术的发展&#xff0c;互联网已经渗透在人们的身边&#xff0c;网络慢慢的变成了人们的生活必不可少的一部分&#xff0c;紧接着网络飞速的发展&#xff0c;系统管理这一名词已不陌生&#xff0c;越来越多的宠物店等机构都会定制一款属于自己个性化…

DWA局部路径规划算法

DWA——Dynamic Window Approach动态窗口法 发展 动态窗口法是一种局部路径规划算法&#xff0c;起源于对移动机器人在复杂环境中实时避障的需求。该算法由F. D. Proentzen和O. Khatib提出&#xff0c;后经过不断优化&#xff0c;已成为移动机器人领域中的标准算法之一。 运…

xss.function靶场(hard)

文章目录 WW3源码分析源码 DOMPpurify框架绕过覆盖变量notifyjs作用域和作用链域构建payload WW3 源码 <!-- Challenge --> <div><h4>Meme Code</h4><textarea class"form-control" id"meme-code" rows"4"><…

Spring Boot实战:使用模板方法模式优化数据处理流程

概述 在软件开发过程中&#xff0c;我们经常需要处理各种各样的数据&#xff0c;这些数据可能来自不同的源&#xff0c;比如数据库、文件系统或者外部API等。尽管数据来源不同&#xff0c;但很多情况下处理这些数据的步骤是相似的&#xff1a;读取数据、清洗数据、转换数据格式…