一。安装mysql

yum list | grep mysql

>>yum install -y mysql-server mysql mysql-devel

CentOS 7的yum源中貌似没有正常安装mysql时的mysql-sever文件。须要去官网上下载

wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

rpm -ivh mysql-community-release-el7-5.noarch.rpm

yum install mysql-community-server

安装成功之后重新启动mysql服务

service mysqld restart

设置开机启动

chkconfig mysqld on

初次安装mysql是root账户是没有password的

设置password的方法

# mysql -uroot
mysql> set password for ‘root’@‘localhost’ = password('mypasswd');
mysql> exit

二,安装MySQL-python

要想使python能够操作mysql 就须要MySQL-python驱动,它是python 操作mysql不可缺少的模块。

wget
http://sourceforge.net/projects/mysql-python/files/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz

tar
zxvf MySQL-python-1.2.3.tar.gz

cd
MySQL-python-1.2.3

$ python setup.py build

$ python setup.py install

三。測试

測试很easy,检查MySQLdb 模块能否够正常导入。

fnngj@fnngj-H24X:~/pyse$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:56)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb

没有报错提示MySQLdb模块找不到,说明安装OK 。以下開始使用python 操作数据库之前。我们有必要来回想一下mysql的基本操作:

第一次设置rootpassword能够使用下面命令:

1 mysqladmin
-u root password NEWPASSWORD

假设你已经设置过password了。须要要下面命令:

1 mysqladmin
-u root -p
'oldpassword' password
newpass

比方说,旧password是“12345”。新password是“nowamagic”。运行下面命令:

1 mysqladmin
-u root -p
'12345' password 'nowamagic'



四。mysql 的基本操作

$ mysql -u root -p  (有password时)

$ mysql -u root     (无password时)

mysql> show databases;  // 查看当前全部的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| csvt |
| csvt04 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.18 sec) mysql> use test; //作用与test数据库
Database changed
mysql> show tables; //查看test库以下的表
Empty set (0.00 sec) //创建user表,name 和password 两个字段
mysql> CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec) //向user表内插入若干条数据
mysql> insert into user values('Tom','1321');
Query OK, 1 row affected (0.05 sec) mysql> insert into user values('Alen','7875');
Query OK, 1 row affected (0.08 sec) mysql> insert into user values('Jack','7455');
Query OK, 1 row affected (0.04 sec) //查看user表的数据
mysql> select * from user;
+------+----------+
| name | password |
+------+----------+
| Tom | 1321 |
| Alen | 7875 |
| Jack | 7455 |
+------+----------+
3 rows in set (0.01 sec) //删除name 等于Jack的数据
mysql> delete from user where name = 'Jack';
Query OK, 1 rows affected (0.06 sec) //改动name等于Alen 的password 为 1111
mysql> update user set password='1111' where name = 'Alen';
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0 //查看表内容
mysql> select * from user;
+--------+----------+
| name | password |
+--------+----------+
| Tom | 1321 |
| Alen | 1111 |
+--------+----------+
3 rows in set (0.00 sec)

五。python 操作mysql数据库基础

#coding=utf-8
import MySQLdb conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor() #创建数据表
#cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))") #插入一条数据
#cur.execute("insert into student values('2','Tom','3 year 2 class','9')") #改动查询条件的数据
#cur.execute("update student set class='3 year 1 class' where name = 'Tom'") #删除查询条件的数据
#cur.execute("delete from student where age='9'") cur.close()
conn.commit()
conn.close()

>>> conn = MySQLdb.connect(host='localhost',port = 3306,user='root', passwd='123456',db ='test',)

Connect() 方法用于创建数据库的连接,里面能够指定參数:username,password,主机等信息。

这仅仅是连接到了数据库,要想操作数据库须要创建游标。

>>> cur = conn.cursor()

通过获取到的数据库连接conn下的cursor()方法来创建游标。

>>> cur.execute("create table student(id int ,name varchar(20),class varchar(30),age varchar(10))")

通过游标cur 操作execute()方法能够写入纯sql语句。

通过execute()方法中写如sql语句来对数据进行操作。

>>>cur.close()

cur.close() 关闭游标

>>>conn.commit()

conn.commit()方法在提交事物,在向数据库插入一条数据时必需要有这种方法,否则数据不会被真正的插入。

>>>conn.close()

Conn.close()关闭数据库连接

六,插入数据

通过上面execute()方法中写入纯的sql语句来插入数据并不方便。如:

>>>cur.execute("insert into student values('2','Tom','3 year 2 class','9')")

我要想插入新的数据,必需要对这条语句中的值做改动。

我们能够做例如以下改动:

#coding=utf-8
import MySQLdb conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor() #插入一条数据
sqli="insert into student values(%s,%s,%s,%s)"
cur.execute(sqli,('3','Huhu','2 year 1 class','7')) cur.close()
conn.commit()
conn.close()

假如要一次向数据表中插入多条值呢?

#coding=utf-8
import MySQLdb conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor() #一次插入多条记录
sqli="insert into student values(%s,%s,%s,%s)"
cur.executemany(sqli,[
('3','Tom','1 year 1 class','6'),
('3','Jack','2 year 1 class','7'),
('3','Yaheng','2 year 2 class','7'),
]) cur.close()
conn.commit()
conn.close()

executemany()方法能够一次插入多条值,运行单挑sql语句,可是反复运行參数列表里的參数,返回值为受影响的行数。

七。查询数据

或许你已经尝试了在python中通过

>>>cur.execute("select * from student")

来查询数据表中的数据。但它并没有把表中的数据打印出来,有些失望。

来看看这条语句获得的是什么

>>>aa=cur.execute("select * from student")

>>>print aa

5

它获得的仅仅是我们的表中有多少条数据。

那如何才干获得表中的数据呢?进入python shell

>>> import MySQLdb
>>> conn = MySQLdb.connect(host='localhost',port = 3306,user='root', passwd='123456',db ='test',)
>>> cur = conn.cursor()
>>> cur.execute("select * from student")
5L
>>> cur.fetchone()
(1L, 'Alen', '1 year 2 class', '6')
>>> cur.fetchone()
(3L, 'Huhu', '2 year 1 class', '7')
>>> cur.fetchone()
(3L, 'Tom', '1 year 1 class', '6')
...
>>>cur.scroll(0,'absolute')

  fetchone()方法能够帮助我们获得表中的数据。但是每次运行cur.fetchone() 获得的数据都不一样。换句话说我没运行一次,游标会从表中的第一条数据移动到下一条数据的位置,所以。我再次运行的时候得到的是第二条数据。

  scroll(0,'absolute') 方法能够将游标定位到表中的第一条数据。

还是没解决我们想要的结果,怎样获得表中的多条数据并打印出来呢?

#coding=utf-8
import MySQLdb conn= MySQLdb.connect(
host='localhost',
port = 3306,
user='root',
passwd='123456',
db ='test',
)
cur = conn.cursor() #获得表中有多少条数据
aa=cur.execute("select * from student")
print aa #打印表中的多少数据
info = cur.fetchmany(aa)
for ii in info:
print ii
cur.close()
conn.commit()
conn.close()

  通过之前的print aa 我们知道当前的表中有5条数据,fetchmany()方法能够获得多条数据。但须要指定数据的条数,通过一个for循环就能够把多条数据打印出啦!运行结果例如以下:

5
(1L, 'Alen', '1 year 2 class', '6')
(3L, 'Huhu', '2 year 1 class', '7')
(3L, 'Tom', '1 year 1 class', '6')
(3L, 'Jack', '2 year 1 class', '7')
(3L, 'Yaheng', '2 year 2 class', '7')
[Finished in 0.1s]

CentOS Python 安装MySQL-python的更多相关文章

  1. python操作三大主流数据库(3)python操作mysql③python操作mysql的orm工具sqlaichemy安装配置和使用

    python操作mysql③python操作mysql的orm工具sqlaichemy安装配置和使用 手册地址: http://docs.sqlalchemy.org/en/rel_1_1/orm/i ...

  2. Python安装MySQL数据库模块

    背景 折腾: [记录]使用Python操作MySQL数据库 的过程中,需要去安装MySQLdb. 下载MySQLdb 去官网: http://pypi.python.org/pypi/MySQL-py ...

  3. python操作三大主流数据库(4)python操作mysql④python服务端flask和前端bootstrap框架结合实现新闻展示

    python操作mysql④python服务端flask和前端bootstrap框架结合实现新闻展示 参考文档http://flask.pocoo.org/docs/0.11/http://flask ...

  4. CentOS 7 安装 MySQL Database

    CentOS 7 安装 MySQL Database 1. 现在安装包,MySQL的安装包被分成了社区版和企业版,而本文将记录社区版本MySQL安装过程,下载MySQL版本如下: mysql-5.7. ...

  5. Centos下安装mysql 总结

    一.MySQL安装 Centos下安装mysql 请点开:http://www.centoscn.com/CentosServer/sql/2013/0817/1285.html 二.MySQL的几个 ...

  6. Centos 7 安装mysql后出现 ERROR 2002 (HY000)解决方案

    Centos 7 安装mysql后出现 ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib ...

  7. CentOS 7 安装MySQL 5.6遇到问题及解决方案

    centos下安装mysql真的没有想象中那么容易,在这里我总结一下遇到的问题 1. ERROR 2002 (HY000): Can’t connect to local MySQL server t ...

  8. Centos下安装mysql 和挂载硬盘

    一,CentOS下安装Mysql 6.5 1.检测系统是否自带安装mysql # yum list installed | grep mysql 2.删除已经安装的Mysql # yum -y rem ...

  9. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  10. 【linux系列】Centos下安装mysql数据库

    前言 为了测试方便,通常我们会自己安装数据库,以下是在Centos上安装Mysql的操作. 一.检查自己是否安装了MySQL数据库 [root@s201 /home/mysql]#rpm -qa |g ...

随机推荐

  1. Berkeley DB Java Edition 简介

    一.             简介        Berkeley DB Java Edition (JE)是一个完全用JAVA写的,它适合于管理海量的,简单的数据. l         能够高效率的 ...

  2. Android开发基础(java)1

    基本概念: 一.结构化方法与结构化程序设计 1.结构化方法:源自迪克斯特拉(E.W.Dijkstra)提出的结构化概念,采用自顶向下.逐步求精的模块化设计方法.核心是将工作分成若干个相互独立的模块,使 ...

  3. [转]谈谈Java中的语法糖

    *该博客转自 http://blog.csdn.net/danchu/article/details/54986442 语法糖(Syntactic Sugar),也称糖衣语法,指在计算机语言中添加的某 ...

  4. LeetCode OJ-- Add Binary

    https://oj.leetcode.com/problems/add-binary/ 对两个二进制字符串求和. 对于字符串 ans_str,求它的翻转. reverse(ans_str.begin ...

  5. autofac 用法总结

    autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...

  6. AtCoder Beginner Contest 084 D - 2017-like Number【数论/素数/前缀和】

    D - 2017-like Number Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Statement W ...

  7. slam学习足迹

    1.slam入门介绍 2.齐次坐标系 3.贝叶斯滤波 均值:平均值 标准差:样本的集中程度/分散度 方差:标准差的平方 协方差:不同维度之间的关系(相关度) 协方差矩阵:多维度之间的关系(相关度) 4 ...

  8. Meet Dgraph — an open source, scalable, distributed, highly available and fast graph databas

    https://dgraph.io/ Meet Dgraph — an open source, scalable, distributed, highly available and fast gr ...

  9. Context都没弄明白,还怎么做Android开发?

    Activity mActivity =new Activity() 作为Android开发者,不知道你有没有思考过这个问题,Activity可以new吗?Android的应用程序开发采用JAVA语言 ...

  10. VS2010中 报错:error C2146、error C4430 原因一:缺少CvvImage类

    今天用vs2010打开vs2008的一个工程,报了好多错: 1>e:\visual studio 2010\projects\imageprojects\morphology\morpholog ...