yum install mysql-serverpython
service mysqld startmysql
[root@LVS1 ~]# mysql -u rootsql
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');數據庫
mysql> show databases;app
mysql> use mysql;ide
mysql> show databases;fetch
mysql>quitui
yum install MySQL-pythonserver
#######################################################ip
#!/usr/bin/env python
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',db='mysql',port=3306)
cur=conn.cursor()
cur.execute('select * from mysql.user')
#result=cur.fetchmany(2)
#result=cur.fetchone()
result=cur.fetchall()
for line in result:
print line,
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'Mysql error Msg:',e
#######################################################往數據庫批量添加數據
#!/usr/bin/env python
#encoding=utf-8
import MySQLdb
try:
conn=MySQLdb.connect(host='localhost',user='root',passwd='123456',port=3306)
cur=conn.cursor()
try:
cur.execute('create database python;')
except:
pass
conn.select_db('python')
try:
cur.execute('create table host(id INT NOT NULL AUTO_INCREMENT,primary key(id),host varchar(20),user varchar(20));')
except:
pass
values=[]
print 'aa'
for i in range(20):
ip='192.168.1.%s'%i
user='USER%s'%i
values.append(('',ip,user))
cur.executemany('insert into host values(%s,%s,%s)',values)
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error,e:
print 'Mysql error Msg:',e
##############################################################