(cls, mysqlMsg)
| 822 | # 检测数据库执行错误 |
| 823 | @classmethod |
| 824 | def IsSqlError(cls, mysqlMsg): |
| 825 | err_dict = { |
| 826 | 1045: "数据库用户名或密码错误!请尝试重置密码后再进行操作!", |
| 827 | 1049: "数据库不存在!", |
| 828 | 1044: "没有指定数据库的访问权限,或指定数据库不存在!", |
| 829 | 1062: "数据库已存在!", |
| 830 | 1142: "指定用户权限不足!", |
| 831 | 1010: "数据库异常,没有权限删除此数据库,请检查是否有非数据库文件或权限是否正常", |
| 832 | 1146: "数据表不存在!", |
| 833 | 2002: "本地服务器无法连接!", |
| 834 | 2003: "数据库服务器连接失败!请检查mysql是否正常启动!", |
| 835 | 23000: "外键约束冲突!", |
| 836 | 6: "数据库异常,没有权限删除此数据库,请检查是否有非数据库文件或权限是否正常!" |
| 837 | } |
| 838 | |
| 839 | import pymysql |
| 840 | if isinstance(mysqlMsg, pymysql.err.MySQLError): # 捕获异常 |
| 841 | error_code, error_message = mysqlMsg.args |
| 842 | |
| 843 | err_msg = err_dict.get(error_code) |
| 844 | if err_msg is not None: |
| 845 | error_message = err_msg + "<pre>" + error_message + "</pre>" |
| 846 | if error_code == 1045: |
| 847 | user_pattern = re.compile(r"for user '(.+?)'@") |
| 848 | user_match = user_pattern.search(error_message) |
| 849 | if user_match: |
| 850 | user = user_match.group(1) |
| 851 | return public.returnMsg(False,"数据库用户名或密码错误 !请尝试重置{}用户密码后再进行操作".format(user)) |
| 852 | if error_code == 1010 or error_code == 6: |
| 853 | dir_pattern = r"'(\./[^']*)'" |
| 854 | dir_match= re.search(dir_pattern,error_message) |
| 855 | if dir_match: |
| 856 | try: |
| 857 | matched_dir = dir_match.group(1).strip('.') |
| 858 | mycnf = public.readFile("/etc/my.cnf") |
| 859 | rep = "datadir\s*=\s*(.+)\n" |
| 860 | data_path = re.search(rep, mycnf).groups()[0] |
| 861 | return public.returnMsg(False,"数据库文件权限异常!请检查存储目录:{} 是否开启防篡改功能,或包含非数据库文件,确认正常后再进行操作".format(data_path+matched_dir)) |
| 862 | except: |
| 863 | pass |
| 864 | |
| 865 | return public.returnMsg(False, error_message) |
| 866 | mysqlMsg = str(mysqlMsg) |
| 867 | if "MySQLdb" in mysqlMsg: return public.returnMsg(False, 'DATABASE_ERR_MYSQLDB') |
| 868 | if "2002," in mysqlMsg or '2003,' in mysqlMsg: return public.returnMsg(False, 'DATABASE_ERR_CONNECT') |
| 869 | if "using password:" in mysqlMsg: return public.returnMsg(False, 'DATABASE_ERR_PASS') |
| 870 | if "Connection refused" in mysqlMsg: return public.returnMsg(False, 'DATABASE_ERR_CONNECT') |
| 871 | if "1133," in mysqlMsg: return public.returnMsg(False, 'DATABASE_ERR_NOT_EXISTS') |
| 872 | if "3679," in mysqlMsg: return public.returnMsg(False, '从数据库删除失败,数据目录不存在!') |
| 873 | if "1141," in mysqlMsg: return public.returnMsg(False, '数据库用户添加失败!') |
| 874 | if "libmysqlclient" in mysqlMsg: |
| 875 | self.rep_lnk() |
| 876 | public.ExecShell("pip uninstall mysql-python -y") |
| 877 | public.ExecShell("pip install pymysql") |
| 878 | public.writeFile('data/restart.pl', 'True') |
| 879 | return public.returnMsg(False, "执行失败,已尝试自动修复,请稍候重试!") |
| 880 | return None |
| 881 |
no test coverage detected