install MySQL Server
yum install mysql-server mysql-devel
Set the MySQL service to start on boot
chkconfig --levels 235 mysqld on
Start the MySQL service
service mysqld start
Test Log into MySQL
mysql -u root*type exit to exit mysql
Install MySQL-python
pip install MySQL-python*you can read "Install Python 2.7.6 on Centos 6.4 x64" for more information
my basic MySQL-python Class
mysql.py
#!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb class classMySQL: def __init__(self): self.db = None def mysql_connect(self, db_user, db_passwd, db_name, db_host='localhost'): self.db = MySQLdb.connect( host = db_host, user = db_user, passwd = db_passwd, db = db_name ); self.query = self.db.cursor() def mysql_close(self): if self.db: self.db.close() def mysql_query(self, query): self.query.execute(query) return self.query.fetchone() # Main must be outside the table class def test(): _mysql = classMySQL() _mysql.mysql_connect("root", "", "mysql") print _mysql.mysql_query("select * from user;") _mysql.mysql_close() if __name__ == '__main__': test()
run test
python2.7 mysql.py
No comments:
Post a Comment