RHEL6.4升级到python2.7.9,然后安装 MySQL-python-1.2.3.tar.gz, 报错。解决错误之后,运行:
python2.7 -c "import MySQLdb"
失败:
>>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/MySQLdb/__init__.py", line 19, in <module> File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py", line 7, in <module> File "/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py", line 6, in __bootstrap__ ImportError: /root/.python-eggs/MySQL_python-1.2.3-py2.7-linux-x86_64.egg-tmp/_mysql.so: undefined symbol: _ZTVN10__cxxabiv120__si_class_type_infoE
找了很多解决方法都无效。转而使用mysql官方的python连接器 mysql-connector-python。下载后安装:
# for mysql5.5+ $ wget http://ftp.ntu.edu.tw/MySQL/Downloads/Connector-Python/mysql-connector-python-2.1.1.tar.gz # for mysql 4.1, 5.0, 5.1+ $ wget http://ftp.ntu.edu.tw/MySQL/Downloads/Connector-Python/mysql-connector-python-1.2.0.tar.gz # or download from my ftp site:然后测试:$ wget ftp://pub:[email protected]/tarball/mysql-connector-python-1.2.0.tar.gz
$ wget ftp://pub:[email protected]/tarball/mysql-connector-python-2.1.1.tar.gz # install it: $ tar zxf mysql-connector-python-1.2.0.tar.gz $ cd mysql-connector-python-1.2.0 $ python2.7 setup.py build $ sudo python2.7 setup.py install
import mysql.connector >>> conn=mysql.connector.connect(user='root', password='******', host='127.0.0.1', database='devops') Traceback (most recent call last): File "<stdin>", line 2, in <module> File "/usr/local/lib/python2.7/site-packages/mysql/connector/__init__.py", line 176, in connect return MySQLConnection(*args, **kwargs) File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 95, in __init__ self.connect(**kwargs) File "/usr/local/lib/python2.7/site-packages/mysql/connector/abstracts.py", line 719, in connect self._open_connection() File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 213, in _open_connection self._ssl) File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 144, in _do_auth self._auth_switch_request(username, password) File "/usr/local/lib/python2.7/site-packages/mysql/connector/connection.py", line 180, in _auth_switch_request raise errors.get_exception(packet) mysql.connector.errors.DatabaseError: 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
解决方法参考:
https://dev.mysql.com/doc/refman/5.6/en/password-expiration-sandbox-mode.html
就是mysql密码过期,重设一下就OK:
mysql> show databases; ERROR 1820 (HY000): You must SET PASSWORD before executing this statement mysql> SET PASSWORD=PASSWORD('new pass here'); Query OK, 0 rows affected (0.00 sec)
然后再试:
>>> config={ ... 'user':'root', ... 'password':'***********', ... 'host':'127.0.0.1', ... 'database':'devops', ... } >>> conn = mysql.connector.connect(**config) >>> cur = conn.cursor() >>> q="SELECT * FROM packages" >>> cur.execute(q) >>> for row in cur: ... print row ... (1, u'kbc', u'krb5-workstation', u'kerberos', None, None, None) (2, u'kbs', u'krb5-server', u'kerberos', None, None, None) (3, u'hadn', u'hadoop-hdfs-datanode', u'hadoop', u'hadoop-hdfs-datanode', u'hann', None) (4, u'hamr', u'hadoop-mapreduce', u'hadoop', None, None, None) (5, u'hanm', u'hadoop-yarn-nodemanager', u'hadoop', u'hadoop-yarn-nodemanager', None, None) (6, u'hajn', u'hadoop-hdfs-journalnode', u'hadoop', u'hadoop-hdfs-journalnode', u'hazk', None) (7, u'hann', u'hadoop-hdfs-namenode', u'hadoop', u'hadoop-hdfs-namenode', u'hajn', None) (8, u'hazk', u'hadoop-hdfs-zkfc', u'hadoop', u'hadoop-hdfs-zkfc', u'zksvr', None) (9, u'halzo', u'hadoop-lzo', u'hadoop', None, None, None) (10, u'halib', u'hadoop-libhdfs', u'halibhdfs', None, None, None) (11, u'halibd', u'hadoop-libhdfs-devel', u'halibhdfs', None, None, None) (12, u'hares', u'hadoop-yarn-resourcemanager', u'hadoop', u'hadoop-yarn-resourcemanager', None, None) (13, u'hahis', u'hadoop-mapreduce-historyserver', u'hadoop', u'hadoop-mapreduce-historyserver', u'hann', None) (14, u'hbma', u'hbase-master', u'hbase', u'hbase-master', u'hann', None) (15, u'hbreg', u'hbase-regionserver', u'hbase', u'hbase-regionserver', u'hbma', None) (16, u'hbrst', u'hbase-rest', u'hbase', None, None, None) (17, u'hbthr', u'hbase-thrift', u'hbase', None, None, None) (18, u'hvbas', u'hive', u'hive', None, None, None) (19, u'hvhb', u'hive-hbase', u'hive', None, None, None) (20, u'hvms', u'hive-metastore', u'hive', u'hive-metastore', None, None) (21, u'hvsvr', u'hive-server2', u'hive', u'hive-server2', None, None) (22, u'hvjdbc', u'hive-jdbc', u'hive', None, None, None) (23, u'hvhc', u'hive-hcatalog', u'hive', None, None, None) (24, u'hvwhs', u'hive-webhcat-server', u'hive', None, None, None) (25, u'hvwhc', u'hive-webhcat', u'hive', None, None, None) (26, u'imcat', u'impala-catalog', u'impala', u'impala-catalog', None, None) (27, u'imsvr', u'impala-server', u'impala', u'impala-server', None, None) (28, u'imsh', u'impala-shell', u'impala', None, None, None) (29, u'imss', u'impala-state-store', u'impala', u'impala-state-store', None, None) (30, u'sphis', u'spark-history-server', u'spark', None, None, None) (31, u'spma', u'spark-master', u'spark', None, None, None) (32, u'spwkr', u'spark-worker', u'spark', None, None, None) (33, u'zkbas', u'zookeeper', u'zookeeper', None, None, None) (34, u'zksvr', u'zookeeper-server', u'zookeeper', u'zookeeper-server', None, None) >>> cur.close() True >>> conn.close() >>>一切正常!
参考:
http://dev.mysql.com/doc/connector-python/en/connector-python-installation-source.html
下载地址:
http://ftp.ntu.edu.tw/MySQL/Downloads/Connector-Python/
官方开发接口参考:
http://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html