【Python】【问题集锦】

1. 用pycharm安装第三方包失败,报类似于“sort"的错误,就转战终端

2. Mac终端安装第三包失败,报类似于“

PermissionError: [Errno 13] Permission denied: '/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/crypto/__init__.py'“的错误: shell命令前加sudo

3. 协程报__sys__:1: RuntimeWarning: coroutine '***' was never awaited:

我代码里导致这个的原因是应该写“from tdqm import tdqm”,我写成了"import tdqm" ,然后直接调用tdqm()方法

4. 2003, "Can't connect to MySQL server on 'localhost' (10061)"

网上查了一些资料,但都解决不了我的问题。偶然看到一篇文章给了我灵感,将host 从 localhost 改成 127.0.0.1 ,(我的前提是 已经安装了 mysql 以及 mysql的客户端 navicat)。问题解决。此类问题网上也可参考:

https://blog.csdn.net/chenhua1125/article/details/79489298  【MySQL远程连接失败(错误码:2003)】

https://www.cnblogs.com/YunnuoFan/p/8482449.html   【mysql连接的特性及拖库姿势】

详细文本,在本页面最下方。

5  Pycharm运行.py文件,控制台多了很多无用信息,是命名问题  

C:\Python35\python3.exe "E:\Program Files\JetBrains\PyCharm 2017.1.1\helpers\pycharm\_jb_unittest_runner.py" --path F:/PythonCode/kaoshi/test.py
Testing started at 20:13 ...
 Launching unittests with arguments python -m unittest discover -s F:/PythonCode/kaoshi -p test.py -t F:\PythonCode\kaoshi in F:\PythonCode\kaoshi
123456

 


Ran 0 tests in 0.000s


OK


Process finished with exit code 0

Empty test suite.

解决办法(参考  https://blog.csdn.net/chenhua1125/article/details/79489298): 

在pycharm中文件名不能命名为test,函数名也不能命名为test,这是因为test在python函数库中是关键字,起了冲突

出现控制台问题后:

正确的是没有Unittests in test.py

python中有很多关键字,出现这样的输出一般为关键字冲突,试着修改函数名或者文件名

 

 【】

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决办法:

在默认情况下,Mysql安装以后会在/tmp目录下生成一个mysql.sock文件,如该文件丢失则Mysql将不能够正常启动,解决方法:使用mysqld_safe 启动即可解决;

复制代码

1
2
3
4
5
#basedir:mysql安装路径,默认在/usr/local/mysql下;datadir:数据库存放路径,默认在/usr/local/mysql/data
#使用下面的命令启动后,会在/tmp/下生成一个mysql.sock文件
#./ 即为:/usr/local/mysql/bin
 
$ ./mysqld_safe  --user=mysql --basedir=/usr/local/mysql  --datadir=/usr/local/mysql/data &

 

 

 

 

【MySQL远程连接失败(错误码:2003)】

一 环境信息

  • 服务器系统:Oracle Linux 7.3
  • 服务器MySQL版本:MySQL 5.7.20
  • 本地系统:win10
  • 本地客户端:Navicat for MySQL 10.1.7
  • 本地开发环境:python(3.6.3),PyMySQL(0.8.0)
  • 假设:登录用户名为admin,密码为adminpwd ,MySQL连接端口3306

二 问题描述

  • 本地客户端及代码连接均失败: 2003, "Can't connect to MySQL server on '192.168.1.166' (10061)"

  • python连接MySQL代码
import pymysql
conn= pymysql.connect(
         host='192.168.1.166',
         port = 3306,
         user='admin',
         passwd='adminpwd',
         db ='test',
         charset='utf8'
         )
# 使用cursor方法获取操作游标
cur = conn.cursor()
# 使用execute 方法执行sql语句
cur.execute("select version()")
# 使用fetchone()方法获取一条数据库
data = cur.fetchone()
print("datebase version : %s"%data)
# 关闭数据库连接
conn.close()
  • python连接MySQL报错
Traceback (most recent call last):    
File "D:/JetBrains/test/study_test/mysql_test1.py", line 15, in <module>
 charset='utf8'
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\__init__.py", line 90, in Connect
 return Connection(*args, **kwargs)
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\connections.py", line 699, in __init__
 self.connect()
File "D:\JetBrains\pyEnv\python363\lib\site-packages\pymysql\connections.py", line 967, in connect
 raise exc
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '192.168.1.166' (timed out)")         

三 官方文档描述

    "The error (2003) Can't connect to MySQL server on 'server' (10061) indicates that the network connection has been refused. You should check that there is a MySQL server running, that it has network connections enabled, and that the network port you specified is the one configured on the server."
    金山译文:错误(2003年)无法连接到“server”(10061)上的mysql服务器,表示网络连接已被拒绝。您应该检查是否有一个mysql服务器正在运行,它是否启用了网络连接,并且您指定的网络端口是在服务器上配置的。

四 解决过程

  • (1) Xshell远程登陆服务器,用“ps aux|grep mysql”命令查看,MySQL服务已启动:

  • (2) 用"vim my.cnf"命令查看my.cnf文件(在MySQL安装目录下,我的位置是/usr/local/mysql/my.cnf),修改其对应的值并重启MySQL。对应内容如下:
[mysqld]
bind-address = 0.0.0.0  # 表示允许任何主机登陆MySQL
port=3306               # 表示MySQL运行端口为3306
  • (3)用“mysql -u admin -p”命令,回车后输入密码“adminpsw”能正常登陆服务器MySQL
mysql> show global variables like 'port';  # 查看MySQL运行的实际端口
+---------------+-------
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.01 sec)
  mysql> use mysql;
  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 host,user from user;
  +-----------+---------------+
  | host      | user          |
  +-----------+---------------+
  | %         | admin         |
  | %         | root          |
  | localhost | mysql.session |
  | localhost | mysql.sys     |
  +-----------+---------------+
  5 rows in set (0.00 sec)
  # 如果上述查询结果,admin用户对应的host不为%,则修改用户权限
  # 此处需注意的是,修改权限时要带上密码(IDENTIFIED BY 'adminpwd'),虽然不知道具体原理,但是没加密码之前客户端还是不能远程访问MySQL。   
  mysql> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%' IDENTIFIED BY 'adminpwd' WITH GRANT OPTION; 
  Query OK, 0 rows affected, 1 warning (0.00 sec)

  mysql> FLUSH PRIVILEGES;
  Query OK, 0 rows affected (0.00 sec)
  • (4) 至此,我的本地Navicat客户端已经能都正常访问服务器端,但是运行上述python连接MySQL代码还是报一样的错误。

  • (5)查网上资料说可能是防火墙屏蔽了3306端口,本地cmd"ping 192.168.1.166"能够Ping通,再用”telnet 192.168.1.166 3306“命令检查端口是否被屏蔽,结果为"正在连接192.168.1.166...无法打开到主机的连接。 在端口 3306: 连接失败",说明是防火墙的问题
    (如果Win10 telnet不是内部或外部命令,决解方法参考连接:https://jingyan.baidu.com/article/1e5468f9033a71484961b7d7.html)

  • (6) 起初我以为是指我本地防火墙的问题,于是把本地防火墙关了,结果问题并没有解决。
  • (7) 其实应该是远程服务器的防火墙问题。远程登陆服务器(我用root用户登录的),检查防火墙状态
systemctl start firewalld # 开启防火墙
systemctl stop firewalld  # 关闭防火墙
systemctl status firewalld  #检查防火墙状态
  • (8) 关闭远端服务器防火墙后,运行本地运行本地python连接MySQL代码,MySQL连接成功
[root@db sysconfig]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since 三 2018-02-28 17:18:10 CST; 7s ago
     Docs: man:firewalld(1)
 Main PID: 5452 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─5452 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
[root@db sysconfig]# systemctl stop firewalld
[root@db sysconfig]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
  • (9)如果你觉得关闭防火墙不安全,可打开远端服务器的iptables(安装或升级命令“yum install iptables”)(我用root用户登录的),并用“vi /etc/sysconfig/iptables”检查3306端口是否打开,如没有,在文件中加入“-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT”(如下所示),保存文件并用“service iptables restart”命令重启iptables
# Generated by iptables-save v1.4.21 on Wed Feb 28 12:19:33 2018
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [34:3136]
-A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT    ## 加上此行
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibite
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Wed Feb 28 12:19:33 2018
~                                                                                                  
~                                                                                                       
"/etc/sysconfig/iptables" 17L, 654C
  • (10)运行本地python连接MySQL代码,结果如下:
datebase version : 5.7.20-enterprise-commercial-advanced
 
 
 
 

  【mysql连接的特性及拖库姿势】

0x01. 前言

有时候,有人会带着这样的疑问?

当netstat -ano发现3306监听在0.0.0.0上,是不是就可以判断mysql是对外开放的

答案当然是否定的。

0x02. mysql连接特性

此时我们可以做一个测试,mysql监听在0.0.0.0上

1

并且服务器的mysql用户设置如下:

3

我们要知道localhost、127.0.0.1、::1这三个host存在优先级,他们优先级以此从高到低。可以这样理解:

如果localhost和127.0.0.1分别设置密码是123和456,那么最后本地登录mysql的密码是123,而不是456

  1.  
    [root@helen ~]# telnet 123.55.91.39 3306
  2.  
    Trying 123.55.91.39...
  3.  
    Connected to 123.55.91.39.
  4.  
    Escape character is '^]'.
  5.  
    GHost '115.29.170.215' is not allowed to connect to this MySQL serverConnection closed by foreign host.

用navicat连接可以看到返回的错误码是1130.也表示不能连接

2


此时我们新增一个非root用户,并且将host改为%,此时可以看到返回的错误码是1045。

5

即:只要host存在%,连接mysql错误的情况都会返回1045错误码。

并且telnet返回的结果如下,还能看到mysql版本是5.5.16-log

  1.  
    [root@helen ~]# telnet 123.55.91.39 3306
  2.  
    Trying 123.55.91.39...
  3.  
    Connected to 123.55.91.39.
  4.  
    Escape character is '^]'.
  5.  
    N
  6.  
    5.5.16-logdt=n4V5D^-d1f0XMYX-mysql_native_passwordConnection closed by foreign host.

0x03. 利用navicat导出的脚本脱裤

navicat在连接数据库的时候可以使用http通道。用法如下:
先将下图导出的脚本文件放到服务器上,通过脚本文件的url来连接。比其他脱裤脚本方便很多。

5

但是win版本的navicat没有这个按钮,mac版的有。win版的在navicat的安装目录下。

9

连接的时候,在http选项中通道地址填写http://123.55.91.39:8089/ntunnel_mysql.php

7

主机名:localhost,这样就可以正常用navicat连接一样

6

最后脱裤的效果:
8

0x04. 总结

    1. mysql返回1130表示没有账号开启外联
    2. mysql返回1045表示有账号开启外联,但是不知道是哪个账号
    3. localhost、127.0.0.1、::1 这三个优先级以此从高到低
 
  •  【. ERROR! The server quit without updating PID file (/usr/local/mysql/data/B0178RQ2019070018.pid).】

B0178RQ2019070018:data wangxue$ chmod -R 777  /usr/local/mysql/data

B0178RQ2019070018:data wangxue$ mysqlstart

Starting MySQL

. SUCCESS! 

B0178RQ2019070018:data wangxue$ mysql -uroot -p

Enter password: (此处输入本机mysql密码即可)

 

  •  【pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 61] Connextion refused)"】

参考:https://www.cnblogs.com/ice5/p/13820474.html

本地数据库能通过root和密码正常登录

逐一排查问题

1,查看端口是否正确(使用的默认端口 3306)

mysql> show global variables like 'port';

 

结果端口为0

2,修改端口号

查看/Library/LaunchDaemons下的com.oracle.oss.mysql.mysqld.plist发现根本就没有Port相关的配置

于是尝试解决;

2.1,停止MySQL服务

sudo /usr/local/mysql/support-files/mysql.server stop

输入本机密码即可

2.2,在com.oracle.oss.mysql.mysqld.plist文件中的ProgramArguments下添加<string>--port=3306</string>保存退出

复制代码
<key>ProgramArguments</key>
        <array>
            <string>/usr/local/mysql/bin/mysqld</string>
            <string>--user=_mysql</string>
            <string>--basedir=/usr/local/mysql</string>
            <string>--datadir=/usr/local/mysql/data</string>
            <string>--plugin-dir=/usr/local/mysql/lib/plugin</string>
            <string>--log-error=/usr/local/mysql/data/mysqld.local.err</string>
            <string>--pid-file=/usr/local/mysql/data/mysqld.local.pid</string>
            <string>--keyring-file-data=/usr/local/mysql/keyring/keyring</string>
            <string>--early-plugin-load=keyring_file=keyring_file.so</string>
            <string>--port=3306</string>
        </array>
复制代码

重启MySQL数据库,再次查看MySQL端口,变为了 3306

启动MySQL服务

sudo /usr/local/MySQL/support-files/mysql.server start

成功!

 
 

 

posted @ 2017-10-02 11:17  素人渔芙2017  阅读(562)  评论(0编辑  收藏  举报