关闭防火墙 systemctl stop firewalld systemctl disable firewalld sed -i 7s/enforcing/disabled/ /etc/selinux/config 安装源码配置需求 dnf install -y gcc gcc-c make cmake zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel libffi-devel tk-devel wget tar vim tree net-tools openssh-server [rootserver ~]# cd /usr/local/src [rootserver src]# tar -zxvf Python-3.11.9.tgz [rootserver src]# cd Python-3.11.9 [rootserver Python-3.11.9]# ./configure --prefix/usr/local/python3.11 --enable-shared [rootserver Python-3.11.9]# make -j$(nproc) make install [rootserver Python-3.11.9]# echo /usr/local/python3.11/lib /etc/ld.so.conf.d/python311.conf [rootserver Python-3.11.9]# ldconfig [rootserver Python-3.11.9]# ln -s /usr/local/python3.11/bin/python3.11 /usr/local/bin/python3 [rootserver Python-3.11.9]# ln -s /usr/local/python3.11/bin/pip3.11 /usr/local/bin/pip3 [rootserver Python-3.11.9]# bash [rootserver Python-3.11.9]# python3 -V [rootserver Python-3.11.9]# pip3 -V [rootserver Python-3.11.9]# python3 -c import ssl; print(ssl.OPENSSL_VERSION) [rootserver ~]# pip3 install --upgrade pip [rootserver ~]# pip3 install pymysql python-dotenv tabulate langchain langchain-openai 配置数据库 [rootserver /]# tar -xvf mysql-8.0.45-linux-glibc2.28-x86_64.tar.xz [rootserver /]# cd mysql-8.0.45-linux-glibc2.28-x86_64 [rootserver mysql-8.0.45-linux-glibc2.28-x86_64]# ls bin docs include lib LICENSE man README share support-files [rootserver mysql-8.0.45-linux-glibc2.28-x86_64] cd / [rootserver /]# mv mysql-8.0.45-linux-glibc2.28-x86_64 /usr/local/mysql [rootserver /]# cd /usr/local/mysql [rootserver yuziang]# groupadd yuziang [rootserver yuziang]# useradd -r -g mysql -s /sbin/nologin mysql [rootserver yuziang]# mkdir data [rootserver yuziang]# chmod -R 750 data [rootserver yuziang]# chown -R mysql:mysql /usr/local/mysql [rootserver yuziang]# bin/mysqld --initialize --usermysql --basedir/usr/local/mysql --datadir/usr/local/mysql/data [rootserver yuziang]# bin/mysqld_safe --usermysql [rootserver ~]# ps -ef | grep mysql [rootserver ~]# cd /usr/local/mysql [rootserver yuziang]# bin/mysql -u root -p [rootserver yuziang]# ln -s /usr/lib64/libncurses.so.6.3 /usr/lib64/libncurses.so.5 [rootserver yuziang]# ln -s /usr/lib64/libtinfo.so.6.3 /usr/lib64/libtinfo.so.5 [rootserver yuziang]# bin/mysql -u root -p Enter password: # 粘贴之前的初始密码 yuziang alter user rootlocalhost identified with mysql_native_password by 123456; yuziang flush privileges; yuziang use mysql; yuziang select user, host, plugin from mysql.user; ---------------------------------------------------- | user | host | plugin | ---------------------------------------------------- | mysql.infoschema | localhost | caching_sha2_password | | mysql.session | localhost | caching_sha2_password | | mysql.sys | localhost | caching_sha2_password | | root | localhost | mysql_native_password | ---------------------------------------------------- 4 rows in set (0.00 sec) yuziangexit [rootserver yuziang]# ps -ef | grep mysql [rootserver yuziang]# kill -9 pid号 [rootserver yuziang]# vim /etc/my.cnf [client] port 3306 socket /tmp/mysql.sock [mysqld] port 3306 basedir /usr/local/mysql datadir /usr/local/mysql/data tmpdir /tmp socket /tmp/mysql.sock character-set-server utf8mb4 collation-server utf8mb4_general_ci default-storage-engineINNODB log_error error.log [rootmaster yuziang]# vim /usr/lib/systemd/system/mysqld.service [Unit] DescriptionMySQL Server Afternetwork.target remote-fs.target nss-lookup.target [Service] Typenotify Usermysql Groupmysql ExecStart/usr/local/mysql/bin/mysqld --defaults-file/etc/my.cnf LimitNOFILE65535 LimitNPROC65535 Restarton-failure RestartPreventExitStatus1 TimeoutSec0 [Install] WantedBymulti-user.target [rootmaster yuziang]# systemctl daemon-reload [rootmaster yuziang]# systemctl enable --now mysqld.service [rootserver yuziang]# vim ~/.bash_profile export PATH$PATH:/usr/local/mysql/bin [rootserver yuziang]# source ~/.bash_profile编写脚本 [rootserver yuziang]# cd [rootserver ~]# mkdir -p /opt/mysql_ai_tools [rootserver ~]# cd /opt/mysql_ai_tools [rootserver yuziang_ai_tools]# touch main.py mysql_client.py prompts.py web_main.py .env vim /opt/mysql_ai_tools/.env MYSQL_HOST127.0.0.1 MYSQL_PORT3306 MYSQL_USERroot MYSQL_PASSWORD123456 MYSQL_DBtestdb LLM_API_KEY自己腾讯云api key LLM_BASE_URLhttps://tokenhub.tencentmaas.com/v1 LLM_MODEL_NAMEdeepseek-v4-pro LLM_TEMPERATURE0 import pymysql import os import re from dotenv import load_dotenv load_dotenv() class Mysql80Client: def __init__(self): self.host os.getenv(MYSQL_HOST, 127.0.0.1) self.port int(os.getenv(MYSQL_PORT, 3306)) self.user os.getenv(MYSQL_USER, root) self.password os.getenv(MYSQL_PASSWORD, ) self.database os.getenv(MYSQL_DB, testdb) self.conn None self.connect() def connect(self): try: self.conn pymysql.connect( hostself.host, portself.port, userself.user, passwordself.password, databaseself.database, charsetutf8mb4, cursorclasspymysql.cursors.DictCursor ) except pymysql.MySQLError as e: raise Exception(f数据库连接失败请检查地址/账号/密码{e.args[1]}) except Exception as e: raise Exception(f数据库连接异常{str(e)}) staticmethod def _check_sql_safety(sql: str) - None: sql_trim sql.strip().upper() danger_keywords [INSERT, UPDATE, DELETE, DROP, ALTER, CREATE, TRUNCATE, REPLACE] for kw in danger_keywords: if re.search(r\b re.escape(kw) r\b, sql_trim): raise Exception(f安全拦截禁止执行 {kw} 类型语句仅支持 SELECT 查询) def execute_query(self, sql: str): self._check_sql_safety(sql) try: with self.conn.cursor() as cursor: cursor.execute(sql) columns [desc[0] for desc in cursor.description] rows cursor.fetchall() return columns, rows except pymysql.MySQLError as e: raise Exception(fSQL执行失败错误码 {e.args[0]}{e.args[1]}) except Exception as e: raise Exception(f查询异常{str(e)}) def get_explain_plan(self, sql: str): self._check_sql_safety(sql) explain_sql fEXPLAIN {sql} try: with self.conn.cursor() as cursor: cursor.execute(explain_sql) columns [desc[0] for desc in cursor.description] rows cursor.fetchall() return columns, rows except pymysql.MySQLError as e: raise Exception(f获取执行计划失败{e.args[1]}) except Exception as e: raise Exception(f执行计划异常{str(e)}) def close(self): if self.conn and not self.conn._closed: self.conn.close()