您的位置:首页 > 健康 > 养生 > 使用frp内网穿透将个人主机上的MySQL发布到公网上,再通过python管理MySQL

使用frp内网穿透将个人主机上的MySQL发布到公网上,再通过python管理MySQL

2025/2/25 8:49:20 来源:https://blog.csdn.net/qq_69920145/article/details/141065488  浏览:    关键词:使用frp内网穿透将个人主机上的MySQL发布到公网上,再通过python管理MySQL

目录

1.frp内网穿透部署

1.frp服务器

1.开放端口

2.上传软件包

3.解压

4.配置文件

2.frp客户端

1.上传软件包

2.配置文件

3.启动测试

1.浏览器查看服务器上连接的客户端数量

2.启动测试

2.MySQL安装

3.python3的安装使用

4.python管理MySQL

1.pip

 2.pandas

3.pymysql

 4.python管理MySQL

5.其他用法


1.frp内网穿透部署

1.frp服务器

1.开放端口
[root@master-mysql ~]# firewall-cmd --zone=public --add-port=7000/tcp --permanent
success
[root@master-mysql ~]# firewall-cmd --zone=public --add-port=7500/tcp --permanent
success
[root@master-mysql ~]# firewall-cmd --zone=public --add-port=6000-6100/tcp --permanent
success
[root@master-mysql ~]# firewall-cmd --reload
success
2.上传软件包

3.解压
[root@localhost ~]# tar -xf frp_0.33.0_linux_amd64.tar.gz 
[root@localhost ~]# cp -r frp_0.33.0_linux_amd64 /usr/local/frp/
[root@localhost ~]# ls /usr/local/frp/
frpc  frpc_full.ini  frpc.ini  frps  frps_full.ini  frps.ini  LICENSE  systemd
4.配置文件
[root@master-mysql ~]# cd /usr/local/frp/
[root@master-mysql frp]# chmod 777 frps
[root@master-mysql frp]# vim ./frps.ini
[common]
bind_port = 7000
dashboard_port = 7500
dashboard_user = abc
dashboard_pwd = abc
token = aaa123

2.frp客户端

1.上传软件包

2.配置文件
[root@localhost ~]# cp -r frp_0.33.0_linux_amd64 /usr/local/frp/
[root@localhost ~]# cd /usr/local/frp/
[common]
server_addr = 192.168.1.40
server_port = 7000
token = aaa123[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 3306
remote_port = 6001

3.启动测试

1.浏览器查看服务器上连接的客户端数量

2.启动测试

如果希望后台运行,请添加nohub和&

服务器启动:

客户端启动:

2.MySQL安装

安装MySQL步骤------看这里

官网下载地址:

MySQL :: MySQL Community Downloads

[root@master-mysql ~]# mysql -P3306 -pQing_tang999
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.33 MySQL Community Server - GPLCopyright (c) 2000, 2023, Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)mysql> create user 'abc'@'%' identified by 'abc';
Query OK, 0 rows affected (0.00 sec)mysql> grant select on test.* to 'abc'@'%';
Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> quit
Bye

 

3.python3的安装使用

在frp的客户端安装python3

[root@localhost ~]# yum -y install python3
[root@localhost ~]# python3 --version
[root@localhost ~]# python3
Python 3.6.8 (default, Nov 14 2023, 16:29:52) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

4.python管理MySQL

1.pip

设置清华镜像站(从国内下载安装包,提高下载和安装速度)

pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

 2.pandas

安装pandas数据分析工具(pandas是知名的数据分析工具, pandas有完整的读取数据的工具,以及DateFrame数据框架,用于保存从数据库中读取的数据)

[root@localhost ~]# pip install pandas

3.pymysql

安装pymysql连接器(oracle为开发者提供的python管理mysql 的工具,通过这个工具,就恶意在不替原有代码的情况下,应对数据库软件的升级)

[root@localhost ~]# pip install pymysql

 4.python管理MySQL

1.查看安装的工具并起别名

>>> pymysql
<module 'pymysql' from '/usr/local/lib/python3.6/site-packages/pymysql/__init__.py'>
>>> pandas
<module 'pandas' from '/usr/local/lib64/python3.6/site-packages/pandas/__init__.py'>
>>> import pymysql as pm
>>> import pandas as pd
>>> pm
<module 'pymysql' from '/usr/local/lib/python3.6/site-packages/pymysql/__init__.py'>
>>> pd
<module 'pandas' from '/usr/local/lib64/python3.6/site-packages/pandas/__init__.py'>
>>> 

由于自己做的MySQL服务器连接出问题了,所以使用了其他的MySQL服务器

2.连接MySQL

>>> conn=pm.connect(
... host='123.249.27.70',   #mysql服务器地址
... user='abcd',            #mysql用户
... password='abcd',        #用户密码
... database='test',        #数据库名称
... port=6001);             #frp客户端使用的端口
>>> conn
<pymysql.connections.Connection object at 0x7f3b41c47ba8>
>>> 

 3.游标---当前读取数据的位置

>>> cursor=conn.cursor()
>>> cursor
<pymysql.cursors.Cursor object at 0x7f3b4d0c6e10>

4.查看数据表

>>> sql="select * from student"
>>> sql
'select * from student'
>>> cursor.execute(sql)
5        #有五行数据

5. 查看数据表的内容,以元组显示


>>> cursor.fetchall()
((1, '章三', '男'), (2, '李四', '女'), (3, '小凤仙', '女'), (4, '章丘铁锅', '男'), (6, '孙颖莎', '女'))

6. 获得数据表的表头

>>> cursor.description
(('id', 3, None, 11, 11, 0, False), ('name', 253, None, 180, 180, 0, False), ('gender', 253, None, 16, 16, 0, False))

7.整理展示---将这些数据放到pandas里,数据会以格子形式显示

>>> head=[]
>>> desc=cursor.description
>>> for var in desc:
...     print(var[0])
... 
id
name
gender
>>> for var in desc:
...     head.append(var[0])
... 
>>> head
['id', 'name', 'gender']
>>> pd.DataFrame(cursor.fetchall)
KeyboardInterrupt
>>> cursor.fetchall()
()
>>> cursor.execute(sql)
5
>>> res=cursor.fetchall(sql)
Traceback (most recent call last):File "<stdin>", line 1, in <module>
TypeError: fetchall() takes 1 positional argument but 2 were given
>>> res=cursor.fetchall()
>>> res
((1, '章三', '男'), (2, '李四', '女'), (3, '小凤仙', '女'), (4, '章丘铁锅', '男'), (6, '孙颖莎', '女'))
>>> pd.DataFrame(data=res,columns=head)id  name gender
0   1    章三      男
1   2    李四      女
2   3   小凤仙      女
3   4  章丘铁锅      男
4   6   孙颖莎      女

 8.使用python文件管理MySQL

[root@localhost ~]# python3 python-mysql.py 
import pymysql as pm
import pandasclass Python_Mysql_01(object):def __init__(self):print("test")def getConn(self):conn=pm.connect(host='123.249.27.70',user='abcd',password='abcd',database='test',port=6001)print(conn)return conndef getRes(self,cursor,sql):cursor.execute(sql)#获得查询的数据data=cursor.fetchall()#表头head=[item[0] for item in cursor.description]#组成pandas数据框DataFramreturn pandas.DataFrame(data=data,columns=head)if __name__=="__main__":#初始化Python_Mysql_01类,创建实例pmppmp=Python_Mysql_01()#获得connconn=pmp.getConn()#获得游标cursor=conn.cursor()df=pmp.getRes(cursor,"select * from student")print(df)[root@localhost ~]# python3 python-mysql.py 
test
<pymysql.connections.Connection object at 0x7f4db1881828>id  name gender
0   1    章三      男
1   2    李四      女
2   3   小凤仙      女
3   4  章丘铁锅      男
4   6   孙颖莎      女

总结:

1.和shell脚本一样,python文件也可称为py脚本,也是将python指令做一个集合

2.为了脚本更加智能化和自动化,添加选择语句(智能化)和循环语句(自动化)

3.同时为了开发效率和可读性做了方法、类、模块

5.其他用法

1. python脚本完成并配置成功之后,将脚本部署为一个二进制的 可执行文件;

2. 因为py文件要被执行需要在linux中安装python环境,但是二进制可执行文件,不要环境,在任何linux主机上都可以执行。

3. 步骤

1. 安装pyinstaller  
pip3 install pyinstaller2. 使⽤pyinstaller⽣成可执⾏⽂件    
pyinstaller --onefile xxx.py
# py⽂件中必须是有 if __name__=="__main__":

python模块 发布web服务

python3 -m http.server 9971

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com