[摘要]deepin加mysql(深度(deepin),关于《deepin加mysql(深度(deepin)》的内容介绍。linux下jdk+tomcat+mysql环境搭建 怎么配置mysql 1 下载并安装一个VMware wo...
deepin加mysql(深度(deepin),关于《deepin加mysql(深度(deepin)》的内容介绍。
linux下jdk+tomcat+mysql环境搭建 怎么配置mysql 1.下载并安装一个VMware workstation, 这个是虚拟机的平台,后面要在里面搭建...
linux下jdk+tomcat+mysql环境搭建 怎么配置mysql
1.下载并安装一个VMware workstation, 这个是虚拟机的平台,后面要在里面搭建Linux系统。
2.下载一个centos安装包,linux版本有多种,比如说redhat、ubuntu、deepin、BT3,个人比较习惯使用centos,这里下载一个 CentOS-6.4-i386-bin-DVD1.iso,6.4版本的。
3.新建一个虚拟机,并把这个安装包导入进去,记住期间步骤有一步是需要建一个用户和设置密码,这里的用户是linux环境的普通账户,但是密码是根账户root和这个普通用户共用的密码
4.安装完成后,是图形化界面,一般我们要用到的是命令行界面,所以这里可以用快捷键 CTRL+ALT+F2,就可以切换到命令界面了。
5.用命令ifconfig查看IP 地址,然后可以考虑用Xshell来连接虚拟机,这样操作命令会比较方便,不用频繁的切换出来或者切换出去。
deepin 2015 安装mysql
1. 下载 MySQL deb 安装包
从这个网址下载自己所需要的mysql包。
我用的deepin,又不想源码安装,就下了debian包。下下来是deb包,ftp上去。
2. 安装 deb 包(这个步骤是从网上找的,经过我安装,是可以的)
sudo dpkg -i mysql-xxxx-xxx.deb
之后你可以去 /opt/mysql/ 目录下找到MySql的服务
3. 使用
groupadd mysql (注意:需要root权限)
useradd -r -g mysql mysql (增加用户)
sudo apt-get install libaio-dev
ln -s /opt/mysql/server-5.6 /usr/local/mysql (软连接)
sudo mkdir /usr/local/mysql/data
sudo chown -R mysql. /usr/local/mysql/data
4.初始化数据库
sudo /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
将mysqld的启动shell程序放到系统服务目录下
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
将 mysql 的配置文件放到系统配置/etc目录下,mysql在全局作用范围要使用该配置文件
sudo cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
5.启动 mysql
sudo /etc/init.d/mysql start
6.更改 mysql root 管理员密码
sudo /usr/local/mysql/bin/mysqladmin -u root password [你希望设置的密码]
7.测试是否可以本地登录 mysql
sudo /usr/local/mysql/bin/mysql -u root -p [可选数据库名称]
8.配置远程
在数据库状态下
mysql GRANT ALL PRIVILEGES ON "." TO "root"@"%" WITH GRANT OPTION;(授权给所有用户可以访问所有库)
mysql FLUSH PRIVILEGES ;
为了避免出现 Access defined这种错误,重置一下root密码
mysql use mysql;
mysql update user set password=password("123456") where user="root";
flush privileges;
mysql exit
完成!
选择centos7还是deepin15 主要是做服务器,安装Apache,MySQL,web网站
1、deepin基于Ubuntu,主要为桌面
2、Centos基于RHEL,主要为服务器
3、另外centos搭建LAMP的文档也很多
4、先安装MySQL,web根据需求连接mysql即可
python怎么操作mysql数据库
如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可。
Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装:
Ubuntu\deepin
sudo apt-get install mysql-server
Sudo apt-get install mysql-client
centOS/redhat
yum install mysql
python mysqldb 同时多少个链接
看数据库的配置和性能
做python的时候需要用到数据库,于是自己重新整理了一下数据库的知识,并且熟悉了python中MysqlDB模块的功能和函数等接口,现在系统地来总结一下吧:
首先你要做的还是下载相应的模块并且安装啦,下载地址自己搜,网上有很多,安装的话也很好办,安装之后python的安装目录下的Lib文件夹下的site-packages文件夹下的MySQLdb文件夹,这之中存放的便是该模块的定义。准备工作做好之后我们需要在源码中import MySQLdb
数据库的连接:
模块引入之后我们就需要和数据库进行连接了,实例代码如下:
db = MySQLdb.connect("localhost","root","123456","myciti" )
这三个关键参数的含义一目了然:第一个为服务器的地址,第二个为用户名,第三个为dbms密码,第四个为要访问的数据库,其实该connect函数的参数不止这些,不过由于其有默认值而且大多数情况下不用修改,因而省略了。这里做如下列表:
host,连接的数据库服务器主机名,默认为本地主机(localhost)。
user,连接数据库的用户名,默认为当前用户。
passwd,连接密码,没有默认值。
db,连接的数据库名,没有默认值。
conv,将文字映射到Python类型的字典。默认为MySQLdb.converters.conversions
cursorclass,cursor()使用的种类,默认值为MySQLdb.cursors.Cursor。
compress,启用协议压缩功能。
named_pipe,在windows中,与一个命名管道相连接。
init_command,一旦连接建立,就为数据库服务器指定一条语句来运行。
read_default_file,使用指定的MySQL配置文件。
read_default_group,读取的默认组。
unix_socket,在unix中,连接使用的套接字,默认使用TCP。
port,指定数据库服务器的连接端口,默认是3306
大家可能会注意到源码中没有用到端口号,这是因为MySQLdb的connect函数的该参数的默认值便是3306,如果你在安装mysql的时候修改了数据库的端口号,那么你就需要在源码中加上该参数的修改值了。
一,安装mysql
如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可。
Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装:
Ubuntu\deepin
sudo apt-get install mysql-server
Sudo apt-get install mysql-client
centOS/redhat
yum install mysql
二,安装MySQL-python
要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块。
下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:
python setup.py install
三,测试
测试非常简单,检查MySQLdb 模块是否可以正常导入。
[email protected]:~/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的基本操作:
四,mysql 的基本操作
$ mysql -u root -p (有密码时)
$ mysql -u root (无密码时)
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-8import 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() 方法用于创建数据库的连接,里面可以指定参数:用户名,密码,主机等信息。
这只是连接到了数据库,要想操作数据库需要创建游标。
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-8import 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-8import 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-8import 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]