显示标签为“mySQL”的博文。显示所有博文
显示标签为“mySQL”的博文。显示所有博文

2008/07/30

Ubuntu: LAMP 安装

没有评论:

LAMP套件就是Linux+Apache+Mysql+PHP这四款软件,组成了一个可以使网站运行的套装工具软件。其中P也可代指 PHP/Python/Perl。如果只用后台数据处理,可以考虑用python编写。当然python 也有网页功能。

版本:
Apache 2
Mysql 5.0
Php 5.1

1.搭建基本的web环境,我使用的是apache2 + php5 + mysql 5.0
sudo apt-get install apache2 mysql-server php5-mysql
2.(optional)解决用户登录时图形验证码无法显示的问题
apt-get install php5-gd
3. 安装mysql的web管理软件phpmyadmin
apt-get install phpmyadmin
4. 基于浏览器的图形化管理工具软件 webwin (http://www.webwin.com)

MySQL更改root密码
刚安装mysql后可执行 mysql -u root -p 加上密码。
方法1:
shell>mysqladmin -u root password new_password

方法2:
mysql> SET PASSWORD FOR root=PASSWORD(’new_password’);

方法3:使用 mysqladmin命令
shell> mysql -u root
mysql> UPDATE user SET Password=PASSWORD(’new_password’) WHERE user=’root’;
mysql> FLUSH PRIVILEGES;

2008/07/07

安装 Python for MySQL 的连接扩展

没有评论:

sudo apt-get install python-mysqldb

MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0 (http://www.python.org/dev/peps/pep-0249/), and is built on top of the MySQL C API. Can discuss using MySQL and Python in forum (http://forums.mysql.com/list.php?50).

一个Python访问mysql的 sample script
1. 建立一个 database 和 一个table:
mysql ->
create database my_db;
use my_db;
create table test(
id int,
name varchar(50)
);
insert into test values(1,”hello”);
insert into test values(2,”world”);

2.Python代码 sample.py

import MySQLdb
class Eb_db:
def __init__(self):
try:
connection = MySQLdb.connect(host=”localhost”, user=”root”, passwd=”123456″, db=”my_db” )
cursor = connection.cursor()
cursor.execute( “SELECT * FROM test ” )
except MySQLdb.OperationalError, message:
errorMessage = “Error %d:\n%s” % (message[ 0 ], message[ 1 ] […]

Another script:

#!/usr/bin/env python
# -*-coding:UTF-8-*-#这一句告诉python用UTF-8编码
"""
***** This is a MySQL test *****

select:
conn=Connection()
conn.select_db('test')
cur=conn.cursor()
cur.execute('select * from user')
cur.scroll(0)
row1=cur.fetchone()
row1[0]
row1[1]
row1[2]

insert:
cur.execute('insert into user (name,passwd) values(\'benyur\',\'12345\')')
cur.insert_id()

update:
cur.execute('update user set passwd=\'123456\' where name=\'benyur\'')

delete:
cur.execute('delete from user where id=2')

**********************************
"""

from MySQLdb import *

def conn():
conn=Connection()
conn.select_db('test')
cur=conn.cursor()
cur.execute('select * from user')
cur.scroll(0)
row1=cur.fetchone()
row1[0]
row1[1]
row1[2]

def usage():
print __doc__

if __name__=='__main__':
usage()

3. 使用
import MySQLdb
3.1. 连接
conn = MySQLdb.Connection(host, user, password, dbname)
3.2. 选择数据库
conn.select_db(’database name’)
3.3. 获得cursor
cur = conn.cursor()
3.4. cursor位置设定
cur.scroll(int, mode)
mode可为相对位置或者绝对位置,分别为relative和absolute。
3.5. select
cur.execute(‘select clause’)
例如
cur.execute(‘select * from mytable’)
row = cur.fetchall()
或者:
row1 = cur.fetchone()
3.6. insert
cur.execute(‘inset clause’)
例如
cur.execute(‘insert into table (row1, row2) values (\’111\’, \’222\’)’)
conn.commit()
3.7. update
cur.execute(‘update clause’)
例如
cur.execute(“update table set row1 = ‘’ where row2 = ‘row2 ‘ ”)
conn.commit()
3.8. delete
cur.execute(‘delete clause’)
例如
cur.execute(“delete from table where row1 = ‘row1’ ”)
conn.commit()

mysqldump 备份:导入和导出

没有评论:

导出整个数据库

mysqldump -u 用户名 -p 数据库名 > 导出的文件名

导入数据库

mysql> create database gsmloc;
mysql> use gsmloc ;
mysql> source gsmloc.sql ;

MySQL 基本知识

一、连接MYSQL。

格式: mysql -h主机地址 -u用户名 -p用户密码
1、例1:连接到本机上的MYSQL。
首先在打开DOS窗口,然后进入目录 mysqlbin,再键入命令mysql -uroot -p,回车后提示你输密码,如果刚安装好MYSQL,超级用户root是没有密码的,故直接回车即可进入到MYSQL中了,MYSQL的提示符是:mysql>
2、例2:连接到远程主机上的MYSQL。假设远程主机的IP为:110.110.110.110,用户名为root,密码为abcd123。则键入以下命令:
mysql -h110.110.110.110 -uroot -pabcd123
(注:u与root可以不用加空格,其它也一样)
3、退出MYSQL命令: exit (回车)

二、修改密码。

格式:mysqladmin -u用户名 -p旧密码 password 新密码
1、例1:给root加个密码ab12。首先在DOS下进入目录mysqlbin,然后键入以下命令
mysqladmin -uroot -password ab12
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
2、例2:再将root的密码改为djg345。
mysqladmin -uroot -pab12 password djg345

三、增加新用户。

(注意:和上面不同,下面的因为是MYSQL环境中的命令,所以后面都带一个分号作为命令结束符)

格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码"
例1、增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询、插入、修改、删除的权限。首先用以root用户连入MYSQL,然后键入以下命令:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
但例1增加的用户是十分危险的,你想如某个人知道test1的密码,那么他就可以在internet上的任何一台电脑上登录你的mysql数据库并对你的数据可以为所欲为了,解决办法见例2。
例2、增加一个用户test2密码为abc,让他只可以在localhost上登录,并可以对数据库mydb进行查询、插入、修改、删除的操作 (localhost指本地主机,即MYSQL数据库所在的那台主机),这样用户即使用知道test2的密码,他也无法从internet上直接访问数据 库,只能通过MYSQL主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
如果你不想test2有密码,可以再打一个命令将密码消掉。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "";

(下篇)
在上篇我们讲了登录、增加用户、密码更改等问题。下篇我们来看看MYSQL中有关数据库方面的操作。注意:你必须首先登录到MYSQL中,以下操作都是在MYSQL的提示符下进行的,而且每个命令以分号结束。

操作技巧
1、如果你打命令时,回车后发现忘记加分号,你无须重打一遍命令,只要打个分号回车就可以了。也就是说你可以把一个完整的命令分成几行来打,完后用分号作结束标志就OK。
2、你可以使用光标上下键调出以前的命令。

四、显示命令

1、显示数据库列表。
show databases;
刚开始时才两个数据库:mysql和test。mysql库很重要它里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。

2、显示库中的数据表:
use mysql;
show tables;


3、显示数据表的结构:
describe 表名;


4、建库:
create database 库名;

5、建表:
use 库名;
create table 表名 (字段设定列表);

6、删库和删表:
drop database 库名;
drop table 表名;


7、将表中记录清空:
delete from 表名;

8、显示表中的记录:
select * from 表名;


五、一个建库和建表以及插入数据的实例

drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default '深圳',
year date
); //建表结束
//以下为插入字段
insert into teacher values('','glchengang','深圳一中','1976-10-10');
insert into teacher values('','jack','深圳一中','1975-12-23');

注:在建表中

(1)将ID设为长度为3的数字字段:int(3)并让它每个记录自动加一:auto_increment并不能为空:not null而且让他成为主字段primary key

(2)将NAME设为长度为10的字符字段

(3)将ADDRESS设为长度50的字符字段,而且缺省值为深圳。varchar和char有什么区别 呢,只有等以后的文章再说了。

(4)将YEAR设为日期字段。

如果你在mysql提示符键入上面的命令也可以,但不方便调试。你可以将以上命令原样写入一个文本文件中假设为school.sql,然后复制到c:\下,并在DOS状态进入目录\mysql\bin,然后键入以下命令:

mysql -uroot -p密码 <>

如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你只要将//的注释去掉即可使用)。


六、将文本数据转到数据库中

1、文本数据应符合的格式:字段数据之间用tab键隔开,null值用\n来代替.
例:
3 rose 深圳二中 1976-10-10
4 mike 深圳一中 1975-12-23

2、数据传入命令 load data local infile "文件名" into table 表名;

注意:你最好将文件复制到\mysql\bin目录下,并且要先用use命令打表所在的库 。


七、备份数据库

mysqldump --opt school>school.bbb

注释:将数据库school备份到school.bbb文件,school.bbb是一个文本文件,文件名任取,打开看看你会有新发现。

2008/04/08

Django开发及mod_python安装

没有评论:

python+Django+apache+mysql 开发网站,different from LAMP which mainly uses PHP

也许大伙都听说过 Ruby on Rails? 一个以 ruby 写的 RAD framework; django 与它大约同期诞生, 两者有着相近的目标, 各自的哲学. (有意思的是: RoR 和 django 的作者之前都是 php 开发方面的高手!)

django 现在官方站点上发布的版本是 0.91, 但是这不过这只是对外的一个静态的小版本, 而其 svn 中每天都有更新, 如果要学习 django 的话, 还是建议从它的 svn 中直接取出最新的版本. 尤其是 magic-removal 分支 - 这将会成为 django 下一个版本: 0.92.

magic-removal 是 django 开发人员们目前正在全力耕作的一个分支 (branch), 最大的目标就是将 django 多年遗留下来的一些疙疙瘩瘩的地方做一次 "大扫除", 以期提升 django 这一开发框架的简洁性与易用性.

如何安装
http://www.djangoproject.com/documentation/install/
简单说,下载version 0.96,解压,然后执行
sudo python setup.py install
如果用于开发,最好之前安装mod_python, which embeds Python within Apache and loads Python code into memory when the server starts. Code stays in memory throughout the life of an Apache process, which leads to significant performance gains over other server arrangements. Make sure you have Apache installed, with the mod_python module activated. Django requires Apache 2.x and mod_python 3.x.

但是据说mod_python和 mod_php冲突,所以现在还在犹豫是否安装Django。本机上原来装有bbPress based on LAMP(php).

Django 中文文档
http://www.yarshure.com/documentation/

Django step by step 中文
http://www.woodpecker.org.cn/obp/django/django-stepbystep/newtest/doc/


Ubuntu下mod_python的安装

目的: 为了能在网页中嵌入python代码
环境: Ubuntu, apache2, python2.5, 并且已安装 apache2-php5 (安装完后php依旧工作,哈哈)

步骤:
首先安装 mod_python .
sudo apt-get install libapache2-mod-python, 或用synaptic装亦可

(按mod_python的说明步骤要安装apxs,但是apache2找不到apxs,因为这种方式已过时了。apache将所有的载入的包都放在
/etc/apache2/mods-enabled 即可加载! 注: apache2的 ServerRoot = /etc/apache2 )

然后检查是否已在 mods-enabled目录下, 如有,则不用执行下面命令:
cd /etc/apache2/mods-enabled/
sudo ln -s ../mods-available/mod_python.load mod_python.load

接着,编辑:
cd /etc/apache2/sites-available/
sudo gedit default

On line 10 you should have:

Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/

加入新行后变为:

Options Indexes FollowSymLinks MultiViews
AllowOverride AuthConfig
Order allow,deny
allow from all

AddHandler mod_python .py
PythonHandler mod_python.publisher
PythonDebug On

# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/

(注: 该文件被链接到 /etc/apache2/sites-enabled/000-default 。)

现在重启 apache server:
sudo /etc/init.d/apache2 restart

测试:
sudo gedit /var/www/test.py
文件内容如下:
def index(req):
return "Test successful";

访问 http://localhost/test.py 应该可以看到 "Test successful" in plain text。


一个显示网页的python程序

import sys
import time

def index(req):

# Following line causes error to be sent to browser
# rather than to log file (great for debug!)

sys.stderr = sys.stdout

#print "Content-type: text/html\n"

#print """
blah1 = """<html>
<head><title>A page from Python</title></head>
<body>
<h4>This page is generated by a Python script!</h4>
The current date and time is """

now = time.gmtime()
displaytime = time.strftime("%A %d %B %Y, %X",now)

#print displaytime,
blah1 += displaytime

#print """
blah1 += """
<hr>
Well House Consultants demonstration
</body>
</html>
"""
return blah1