天天看点

LAMP

lamp

lamp是目前最为成熟的一种企业网站应用模式,可以提供站点应用及开发环境

构成组件:linux apache mysql php/perl/pyhon

安装环境

需要开发工具和开发库支持

[root@localhost ~]# tar zxvf httpd-2.2.9.tar.gz -c /usr/src/

[root@localhost ~]# cd /usr/src/httpd-2.2.9/

[root@localhost httpd-2.2.9]# ./configure --prefix=/usr/local/apache2 --enable-so --enable-rewrite --enable-ssl --with-ssl=/usr/lib --enable-auth-digest --enable-cgi --enable-suexec --with-suexec-caller=daemon --with-suexec-docroot=/usr/local/apache2/htdocs

--enable-ssl:开启ssl

--enable-auth-digest:apache有两种认证:基本和摘要,这里选择摘要认证

--enable-cgi:启用cgi功能

--enable-suexec:启用suexec,postfix 要用到

……..//省略显示

config.status: creating docs/conf/extra/httpd-manual.conf

config.status: creating docs/conf/extra/httpd-mpm.conf

config.status: creating docs/conf/extra/httpd-multilang-errordoc.conf

config.status: creating docs/conf/extra/httpd-ssl.conf

config.status: creating docs/conf/extra/httpd-userdir.conf

config.status: creating docs/conf/extra/httpd-vhosts.conf

config.status: creating include/ap_config_layout.h

config.status: creating support/apxs

config.status: creating support/apachectl

config.status: creating support/dbmmanage

config.status: creating support/envvars-std

config.status: creating support/log_server_status

config.status: creating support/logresolve.pl

config.status: creating support/phf_abuse_log.cgi

config.status: creating support/split-logfile

config.status: creating build/rules.mk

config.status: creating build/pkg/pkginfo

config.status: creating build/config_vars.sh

config.status: creating include/ap_config_auto.h

config.status: executing default commands

[root@localhost httpd-2.2.9]# make

…..//省略显示信息

.9/srclib/apr/libapr-1.la -luuid -lrt -lcrypt -lpthread -ldl

cd support && make suexec

make[2]: entering directory `/usr/src/httpd-2.2.9/support'

/usr/src/httpd-2.2.9/srclib/apr/libtool --silent --mode=compile gcc -g -o2 -pthread    -dlinux=2 -d_reentrant -d_gnu_source -d_largefile64_source    -i/usr/src/httpd-2.2.9/srclib/pcre -i. -i/usr/src/httpd-2.2.9/os/unix -i/usr/src/httpd-2.2.9/server/mpm/prefork -i/usr/src/httpd-2.2.9/modules/http -i/usr/src/httpd-2.2.9/modules/filters -i/usr/src/httpd-2.2.9/modules/proxy -i/usr/src/httpd-2.2.9/include -i/usr/src/httpd-2.2.9/modules/generators -i/usr/src/httpd-2.2.9/modules/mappers -i/usr/src/httpd-2.2.9/modules/database -i/usr/src/httpd-2.2.9/srclib/apr/include -i/usr/src/httpd-2.2.9/srclib/apr-util/include -i/usr/src/httpd-2.2.9/modules/proxy/../generators -i/usr/lib/include -i/usr/kerberos/include -i/usr/src/httpd-2.2.9/modules/ssl -i/usr/src/httpd-2.2.9/modules/dav/main  -prefer-non-pic -static -c suexec.c && touch suexec.lo

/usr/src/httpd-2.2.9/srclib/apr/libtool --silent --mode=link gcc -g -o2 -pthread     -l/usr/lib   -o suexec suexec.lo

make[2]: leaving directory `/usr/src/httpd-2.2.9/support'

make[1]: leaving directory `/usr/src/httpd-2.2.9'

[root@localhost httpd-2.2.9]# make install

…….//省略显示信息

installing configuration files

mkdir /usr/local/apache2/conf

mkdir /usr/local/apache2/conf/extra

mkdir /usr/local/apache2/conf/original

mkdir /usr/local/apache2/conf/original/extra

installing html documents

mkdir /usr/local/apache2/htdocs

installing error documents

mkdir /usr/local/apache2/error

installing icons

mkdir /usr/local/apache2/icons

mkdir /usr/local/apache2/logs

installing cgis

mkdir /usr/local/apache2/cgi-bin

installing header files

installing build system files

installing man pages and online manual

mkdir /usr/local/apache2/man

mkdir /usr/local/apache2/man/man1

mkdir /usr/local/apache2/man/man8

mkdir /usr/local/apache2/manual

[root@localhost httpd-2.2.9]# cd

[root@localhost ~]# /usr/local/apache2/bin/apachectl start

[root@localhost ~]# cp /usr/local/apache2/bin/apachectl /etc/init.d/httpd

[root@localhost ~]# service httpd restart

[root@localhost ~]# chkconfig --add httpd

httpd 服务不支持 chkconfig

[root@localhost ~]# vim /etc/init.d/httpd

#!/bin/sh

#chkconfig: 35 85 15

#description: new http server daemon

[root@localhost ~]# chkconfig --list httpd

httpd           0:关闭  1:关闭  2:关闭  3:启用  4:关闭  5:启用  6:关闭

mysql编译安装

[root@localhost ~]# useradd -u 400 -m -s /sbin/nologin mysql  //创建mysql的运行身份

[root@localhost ~]# tail -1 /etc/passwd       //查看passwd

mysql:x:400:400::/home/mysql:/sbin/nologin

[root@localhost ~]# tar zxvf mysql-5.0.56.tar.gz -c /usr/src/

[root@localhost ~]# cd /usr/src/mysql-5.0.56/

[root@localhost mysql-5.0.56]# ./configure --prefix=/usr/local/mysql

……//省略显示信息

config.status: creating read/makefile

config.status: creating rem/makefile

config.status: creating row/makefile

config.status: creating srv/makefile

config.status: creating sync/makefile

config.status: creating thr/makefile

config.status: creating trx/makefile

config.status: creating usr/makefile

config.status: creating ib_config.h

config.status: ib_config.h is unchanged

config.status: executing depfiles commands

mysql has a web site at http://www.mysql.com/ which carries details on the

latest release, upcoming features, and other information to make your

work or play with mysql more productive. there you can also find

information about mailing lists for mysql discussion.

remember to check the platform specific part of the reference manual for

hints about installing mysql on your platform. also have a look at the

files in the docs directory.

thank you for choosing mysql!

[root@localhost mysql-5.0.56]# make

……..//省略显示信息

        then mv -f ".deps/parse_output.tpo" ".deps/parse_output.po"; else rm -f ".deps/parse_output.tpo"; exit 1; fi

/bin/sh ../../libtool --preserve-dup-deps --tag=cxx --mode=link g++  -o3 -ddbug_off    -fno-implicit-templates -fno-exceptions -fno-rtti  -rdynamic  -o mysqlmanager  command.o mysqlmanager.o manager.o log.o thread_registry.o listener.o protocol.o mysql_connection.o user_map.o messages.o commands.o instance.o instance_map.o instance_options.o buffer.o parse.o guardian.o parse_output.o liboptions.la libnet.a ../../vio/libvio.a ../../mysys/libmysys.a ../../strings/libmystrings.a ../../dbug/libdbug.a -lz -lpthread -lcrypt -lnsl -lm  -lpthread

g++ -o3 -ddbug_off -fno-implicit-templates -fno-exceptions -fno-rtti -rdynamic -o mysqlmanager command.o mysqlmanager.o manager.o log.o thread_registry.o listener.o protocol.o mysql_connection.o user_map.o messages.o commands.o instance.o instance_map.o instance_options.o buffer.o parse.o guardian.o parse_output.o  ./.libs/liboptions.a -lpthread -lpthread -lpthread -lpthread libnet.a ../../vio/libvio.a ../../mysys/libmysys.a ../../strings/libmystrings.a ../../dbug/libdbug.a -lz -lpthread -lcrypt -lnsl -lm -lpthread

make[3]: leaving directory `/usr/src/mysql-5.0.56/server-tools/instance-manager'

make[3]: entering directory `/usr/src/mysql-5.0.56/server-tools'

make[3]: nothing to be done for `all-am'.

make[3]: leaving directory `/usr/src/mysql-5.0.56/server-tools'

make[2]: leaving directory `/usr/src/mysql-5.0.56/server-tools'

make[1]: leaving directory `/usr/src/mysql-5.0.56'

[root@localhost mysql-5.0.56]# make install

making install in instance-manager

make[3]: entering directory `/usr/src/mysql-5.0.56/server-tools/instance-manager'

make[4]: entering directory `/usr/src/mysql-5.0.56/server-tools/instance-manager'

test -z "/usr/local/mysql/libexec" || mkdir -p -- "/usr/local/mysql/libexec"

  /bin/sh ../../libtool --preserve-dup-deps --mode=install /usr/bin/install -c 'mysqlmanager' '/usr/local/mysql/libexec/mysqlmanager'

/usr/bin/install -c mysqlmanager /usr/local/mysql/libexec/mysqlmanager

make[4]: nothing to be done for `install-data-am'.

make[4]: leaving directory `/usr/src/mysql-5.0.56/server-tools/instance-manager'

make[4]: entering directory `/usr/src/mysql-5.0.56/server-tools'

make[4]: nothing to be done for `install-exec-am'.

make[4]: leaving directory `/usr/src/mysql-5.0.56/server-tools'

you have mail in /var/spool/mail/root

 建立配置文件:在展开的源代码包目录中的“support-files”文件夹下,提供了多个mysql服务器的配置样例文件,分别适用于不同负载的数据库服务器。一般选择my-medium.cnf文件即可,该配置文件适用于中等负载的数据库,可以满足大多数企业的应用需求

[root@localhost mysql-5.0.56]# cp support-files/my-medium.cnf /etc/my.cnf

 初始化数据库:以mysql用户的身份执行mysql_install_db脚本,对mysql数据库进行初始化。修改相关的所有权,以便mysql用户读写数据库

[root@localhost mysql-5.0.56]# /usr/local/mysql/bin/mysql_install_db --user=mysql

you can start the mysql daemon with:

cd /usr/local/mysql ; /usr/local/mysql/bin/mysqld_safe &

you can test the mysql daemon with mysql-test-run.pl

cd mysql-test ; perl mysql-test-run.pl

please report any problems with the /usr/local/mysql/bin/mysqlbug script!

the latest information about mysql is available on the web at

http://www.mysql.com

support mysql by buying support/licenses at http://shop.mysql.com

[root@localhost mysql-5.0.56]# chown -r root.mysql /usr/local/mysql/ //修改目录的所有权

[root@localhost mysql-5.0.56]# ls -ld /usr/local/mysql/  //查看结果

drwxr-xr-x 12 root mysql 4096 07-20 20:06 /usr/local/mysql/

[root@localhost mysql-5.0.56]# chown -r mysql /usr/local/mysql/var/ //修改目录的所有权

[root@localhost mysql-5.0.56]# ls -ld /usr/local/mysql/var/  //查看结果

drwx------ 4 mysql mysql 4096 07-20 20:06 /usr/local/mysql/var/

 调整lib库路径:由于mysql安装到了非标准的路径中。所以还需要将mysql的库文件路径“/usr/local/mysql/lib/mysql”加入到系统的库文件搜索路径中,以便在用到时能够自动搜索到。增加库文件搜索路径可以通过修改“/etc/ld.so.conf”文件实现

[root@localhost mysql-5.0.56]# echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf

[root@localhost mysql-5.0.56]# ldconfig  //刷新库文件搜索路径,使修改生效

 mysql启动控制

在“/usr/local/mysql/bin”目录中存放着管理mysql服务器的脚本和程序。其中脚本文件mysqld_safe可以用来安全启动mysql服务器,结合命令选项“--user”可指定运行服务的用户身份。mysql服务器默认使用tcp 3306端口提供服务。

[root@localhost mysql-5.0.56]# /usr/local/mysql/bin/mysqld_safe --user=mysql & //&的作用是后台

[root@localhost mysql-5.0.56]# netstat -ntpl | grep 3306

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   listen      16439/mysqld       

 如果需要修改mysqld服务监听的端口,可以编辑“/ect/my.cnf”配置文件,根据需要修改“[mysqld]”配置段中的“port”行,例如改为“port=12345”

 将mysqld添加为系统服务

在展开的mysql源码包目录中的“support-files”文件夹下,“mysql.server”文件可用来作为mysqld服务器的启动脚本。将其复制到“/etc/init.d/”目录,并注意添加执行权限(否则在执行“service mysqld start”时系统会提示“mysqld:unrecognized  service”)使用带“--add”选项的chkconfig命令将其设为系统自动启动服务即可。

[root@localhost mysql-5.0.56]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mysql-5.0.56]# chmod +x /etc/init.d/mysqld

[root@localhost mysql-5.0.56]# service mysqld restart

shutting down mysql                                        [确定]

starting mysql                                             [确定]

[root@localhost mysql-5.0.56]# chkconfig --add mysqld

[root@localhost mysql-5.0.56]# chkconfig --level 35 mysqld on

[root@localhost mysql-5.0.56]# chkconfig --list mysqld

mysqld          0:关闭  1:关闭  2:关闭  3:启用  4:关闭  5:启用  6:关闭

 以后就可以分别使用 “start”“stop”“restart”差数的service命令来启动,关闭及重启mysqld服务

 设置mysql程序的执行路径

为了在执行“mysql”命令和脚本工具时输入更加方便,可以修改path环境变量,添加执行路径“/usr/local/mysql/bin”,并将相关设置定义到系统的“/etc/profile”文件中去。

[root@localhost mysql-5.0.56]# cd

[root@localhost ~]# export path=$path:/usr/local/mysql/bin

[root@localhost ~]# echo "path=$path:/usr/local/mysql/bin" >> /etc/profile

数据库基本管理

 使用mysql客户端命令工具“mysql”可以可以连接并登陆到mysql环境,在带有提示符“mysql>”的交互式命令环境中进行操作。在该操作环境中,输入的每一条数据库管理命令以分号“;”表示结束,可以不区分大小写(但习惯上将sql语句中的关键字使用大写)。

 对于刚初始化完毕的mysql数据库服务器来说,其管理账号root默认是没有设置密码的。只要直接执行“mysql –u root”命令即以root用户身份登陆本机的mysql服务器,在“mysql>”环境中,输入“exit”命令即可退出。

[root@localhost ~]# mysql -u root

welcome to the mysql monitor.  commands end with ; or \g.

your mysql connection id is 1

server version: 5.0.56-log source distribution

type 'help;' or '\h' for help. type '\c' to clear the buffer.

mysql> exit

bye

[root@localhost ~]#

 为了安全起见,使用mysqladmin命令工具为mysql数据库的root用户设置一个密码。 再次连接mysqld服务时,需要在“mysql –u root”命令后加上“-p”参数,根据系统提示输入正确的密码后才可以登录,mysql环境

 将root用户的密码设置为“qq123.com”并再次登录mysql服务器。

[root@localhost ~]# mysqladmin -u root password "qq123.com"

[root@localhost ~]# mysql -u root -p

enter password:

your mysql connection id is 4

mysql>

 注意,这个root用户是来自访问mysqld服务的,而不是登录linux系统的root用户

显示数据库结构

 show databases语句;用于查看mysql服务器中包括的数据库列表。经初始化后的mysql服务器,默认建立了三个数据库“tast,mysql,information_schema”

mysql> show databases;

+--------------------+

| database           |

| information_schema |

| mysql              |

| test               |

3 rows in set (0.00 sec)

查看数据库中的数据表信息

 show tables语句:用于显示当前数据库中包含 的数据表。在显示数据表之前,需要先使用“use”语句切换到所使用的库

 切换到mysql。并显示其中包含有哪些数据表

mysql> use mysql;

database changed

mysql> show tables;

+---------------------------+

| tables_in_mysql           |

| columns_priv              |

| db                        |

| func                      |

| help_category             |

| help_keyword              |

| help_relation             |

| help_topic                |

| host                      |

| proc                      |

| procs_priv                |

| tables_priv               |

| time_zone                 |

| time_zone_leap_second     |

| time_zone_name            |

| time_zone_transition      |

| time_zone_transition_type |

| user                      |

17 rows in set (0.00 sec)

 mysql数据库的数据文件存储在目录“/usr/local/mysql/var/”中,每个数据库对应一个目录,用于存放数据表文件,每一个数据表为三个文件,后缀名分别为“.frm”“.myd”“.myi”。

 查看数据库mysql中的user表对应的文件数据

[root@localhost ~]# ls /usr/local/mysql/var/mysql | grep user

user.frm

user.myd

user.myi

 显示数据表的结构

descrlbe语句:用于显示数据表中列表的信息(或者表结构),以“数据库名.表名”作为参数,命令格式为:

describe [数据库名.]表名

当省略数据库名时,则认为是查询当前数据库中的表

mysql> describe user;

+-----------------------+-----------------------------------+------+-----+---------+-------+

| field                 | type                              | null | key | default | extra |

| host                  | char(60)                          | no   | pri |         |       |

| user                  | char(16)                          | no   | pri |         |       |

| password              | char(41)                          | no   |     |         |       |

| select_priv           | enum('n','y')                     | no   |     | n       |       |

| insert_priv           | enum('n','y')                     | no   |     | n       |       |

| update_priv           | enum('n','y')                     | no   |     | n       |       |

| delete_priv           | enum('n','y')                     | no   |     | n       |       |

| create_priv           | enum('n','y')                     | no   |     | n       |       |

| drop_priv             | enum('n','y')                     | no   |     | n       |       |

| reload_priv           | enum('n','y')                     | no   |     | n       |       |

| shutdown_priv         | enum('n','y')                     | no   |     | n       |       |

| process_priv          | enum('n','y')                     | no   |     | n       |       |

| file_priv             | enum('n','y')                     | no   |     | n       |       |

| grant_priv            | enum('n','y')                     | no   |     | n       |       |

| references_priv       | enum('n','y')                     | no   |     | n       |       |

| index_priv            | enum('n','y')                     | no   |     | n       |       |

| alter_priv            | enum('n','y')                     | no   |     | n       |       |

| show_db_priv          | enum('n','y')                     | no   |     | n       |       |

| super_priv            | enum('n','y')                     | no   |     | n       |       |

| create_tmp_table_priv | enum('n','y')                     | no   |     | n       |       |

| lock_tables_priv      | enum('n','y')                     | no   |     | n       |       |

| execute_priv          | enum('n','y')                     | no   |     | n       |       |

| repl_slave_priv       | enum('n','y')                     | no   |     | n       |       |

| repl_client_priv      | enum('n','y')                     | no   |     | n       |       |

| create_view_priv      | enum('n','y')                     | no   |     | n       |       |

| show_view_priv        | enum('n','y')                     | no   |     | n       |       |

| create_routine_priv   | enum('n','y')                     | no   |     | n       |       |

| alter_routine_priv    | enum('n','y')                     | no   |     | n       |       |

| create_user_priv      | enum('n','y')                     | no   |     | n       |       |

| ssl_type              | enum('','any','x509','specified') | no   |     |         |       |

| ssl_cipher            | blob                              | no   |     | null    |       |

| x509_issuer           | blob                              | no   |     | null    |       |

| x509_subject          | blob                              | no   |     | null    |       |

| max_questions         | int(11) unsigned                  | no   |     | 0       |       |

| max_updates           | int(11) unsigned                  | no   |     | 0       |       |

| max_connections       | int(11) unsigned                  | no   |     | 0       |       |

| max_user_connections  | int(11) unsigned                  | no   |     | 0       |       |

37 rows in set (0.03 sec)

创建新的数据库

create database语句;用于创建一个新的数据库,使用数据库名称作为参数

命令格式为:create database 数据库名

创建一个名为“auth”的数据库

mysql> create database auth;

query ok, 1 row affected (0.06 sec)

 创建新的数据库需要有足够的权限,且数据库名称必须唯一。刚建立的数据库是空的不包含任何表,在“/usr/local/mysql/var”目录下会对应有一个与数据库名称相同的目录

 创建新的数据表

create table语句:用于在数据库中创建新的数据表,需要使用数据表名称作为参数。命令格式为

 create table 表名 (字段定义……..)

例如:在auth库中创建一个名为users的表。包括两字段user_name,user_passwd,均为非空字符串值,初始化密码值设为“123456”其中user_name字段被设置为关键索引字段(primary key)。

mysql> use auth;

mysql> create table users (user_name char(30) not null, user_passwd char(20) not null default '123456', primary key (user_name));

query ok, 0 rows affected (0.00 sec)

删除一个数据表

drop table语句:用于在数据库中删除指定的数据表,需要使用数据表名称作为参数,命令格式为:

drop table [数据库名.]表名

mysql> drop table auth.users;

query ok, 0 rows affected (0.01 sec)

 删除一个数据库

drop database语句:用于删除指定的数据库,需要使用数据库名称作为参数,命令格式为:

drop database 数据库名

 删除auth数据库

mysql> drop database auth;

query ok, 0 rows affected (0.02 sec)

 数据录入与维护

例:创建auth数据库和users表下面将以此为基础,学习如何使用insert, select, update, delete语句向数据库表中插入,查询, 修改,及删除数据记录。

(1)插入新的数据记录

insert语句:用于向数据库 中插入新的数据记录,命令格式为:

insert into 表名(字段1,字段2,……) values(字段1的值,字段2的值,…………);

 在auth库下的users表中插入两个用户zhangsan,lisi的记录,密码分别为“123456”“654321”

query ok, 1 row affected (0.00 sec)

mysql> insert into auth.users(user_name,user_passwd) values('zhangsan', encrypt('123456'));

query ok, 1 row affected (0.03 sec)

mysql> insert into auth.users(user_name,user_passwd) values('lisi', encrypt('654321'));

查询数据记录

select语句:用于从数据表中查 找符合条件的数据记录,mysql数据库支持标准的sql查询语句。命令格式为:

select 字段名1,字段名2……… from 表名 where 条件表达式

表示所有字段时可以使用通配符“*”,不指定特定条件时可以省略“where”部分

例:在auth库的users表中,查看所有的数据记录

mysql> select * from auth.users;

+-----------+---------------+

| user_name | user_passwd   |

| zhangsan  | 0044eofkxmlx2 |

| lisi      | a1obma2vswboc |

2 rows in set (0.00 sec)

密码字段的值显示为加密的字符串信息

在auth库的users表中,查询用户名为“zhangsan”的记录,输出用户名,密码字段

mysql> select user_name,user_passwd from auth.users where user_name='zhangsan';

1 row in set (0.00 sec)

修改数据库记录

update语句:用于修改。更新数据表中的数据记录。命令格式为:

update 表名 set 字段名1=字段值1[字段名2=字段值2]  where 条件表达式

例:将数据库用户lisi的密码改为“123456”

mysql> update auth.users set user_passwd=encrypt('123456') where user_name='lisi';

query ok, 1 row affected (0.02 sec)

rows matched: 1  changed: 1  warnings: 0

 更改root管理员的密码,更改完执行“flush privileges”语句刷新用户授权信息

mysql> update mysql.user set password=password('123456') where user='root';

query ok, 3 rows affected (0.01 sec)

rows matched: 3  changed: 3  warnings: 0

mysql> flush privileges;

删除数据库记录

delete from 表名where 条件表达式

例:在auth库的users表中,删除name字段的值为“lisi”的数据记录

mysql> delete from auth.users where user_name='lisi';

query ok, 1 row affected (0.01 sec)

为了安全删除mysql的空用户

mysql> delete from mysql.user where user='';

query ok, 2 rows affected (0.00 sec)

维护数据库及用户权限

 数据库的备份与恢复

及时备份数据库是信息安全管理的重要工作内容之一。mysql数据备份可以使用多种方式,直接备份数据库目录“/var/local/mysqlvar”是一种比较快捷的方式,而更广泛使用的做法是使用mysqldump命令来完成备份

 使用mysqldump命令可将数据库信息导出为sql脚本,这样的脚本文件还能够在不同版本的mysql服务器上使用。例如,当需要升级mysql数据库软件的版本时,使用mysqldump命令将原有数据库信息导出, 直接在更新后的mysql服务器中导入即可

 mysqldump命令可以完成全部数据库,指定数据库,数据表的备份。命令格式为:

mysqldump –u 用户名 –p [密码] [options] [表名] > /备份路径/备份文件名

 备份整个auth数据库

[root@localhost ~]# mysqldump -u root -p auth > mysql-auth.sql

[root@localhost ~]# ll mysql-auth.sql

-rw-r--r-- 1 root root 1826 01-18 17:19 mysql-auth.sql

备份数据库mysql中的user表,host表。

[root@localhost ~]# mysqldump -u root -p mysql host user > mysql.host-user.sql

[root@localhost ~]# ll mysql.host-user.sql

-rw-r--r-- 1 root root 6742 01-18 17:21 mysql.host-user.sql

备份mysql服务器中所有数据库的内容(添加“—all-datebases”),当需要备份的信息较多时可以添加“--opt”选项进行优化,以较快备份速度

[root@localhost ~]# mysqldump -u root -p --all-databases > mysql-all.sql

[root@localhost ~]# ll mysql-all.sql

-rw-r--r-- 1 root root 398694 01-18 17:27 mysql-all.sql

恢复数据库

 对于使用mysqldump命令导出的备份文件,在需要 恢复时可以直接通过mysql命令进行倒入。使用mysql命令导入.sql脚本文件,命令格式为

mysql –u root –p [数据库名] < /备份路径/备份文件名

备份文件mysql-all.sql包括所有的(或多个)数据库信息时,执行mysql导入时可以不指定数据库名

[root@localhost ~]# mysql -u root -p < mysql-all.sql

备份文件之包括单个数据库或单个数据表时,执行mysql导入时需要指定目标数据库的名称

[root@localhost ~]# mysql -u root -p < mysql-auth.sql  //没有指定数据库出现错误如下

error 1046 (3d000) at line 18: no database selected

[root@localhost ~]# mysql -u root -p auth < mysql-auth.sql

[root@localhost ~]# mysql -u root -p mysql < mysql.host-user.sql

用户及权限设置

 mysql数据库用户的全新管理主要包括三个方面:授予权限,查看权限,撤销权限

 授予权限

grant语句使用来创建用户并进行授权的最常用语句。当指定的用户名不存在时grant语句将会创建新的用户,否则用于修改用户信息。命令格式为:

grant 权限列表 on 数据库名.表名 to 用户名@来源地址 [ identified by ‘密码’ ]

 使用grant语句时,需要注意的事项如下

权限列表:使用“all”关键字代表全部权限,同时授予多个权限时,以逗号“,”分隔,例如“select,insert,update,delete”

 表名:可使用通配符“*”表示指定数据库中所有数据表

 用户名@来源地址:用来设置谁能连接,从哪里连接。用户名不能使用通配符,但使用连续的两单引号“ '’ ”时表示空字符串,可用于匹配任何用户。来源地址表示连接数据库的客户机地址,可以使用“%”作为通配符,匹配某个域内的所有地址(如 %,benet.com),或使用带掩码标记的网络地址(如192.168.1.0/24)。

identified by:用于设置用户连接数据库时使用的密码字符串,密码进过加密后存储与mysql库的user表中,省略“identified by”部分时,新用户的密码将为空。

 例:授权数据库用户qiao,允许其从本机连接到mysql服务器,只能查看mysql库中的user表的内容,使用密码‘“123456”进行验证。

mysql> grant select on mysql.user to qiao@'localhost' identified by '123456';

使用用户qiao的身份连接数据库,当查看非授权的数据表示将被拒绝

[root@localhost ~]# mysql -u qiao -p

……//省略信息

mysql> select * from mysql.sb;

error 1142 (42000): select command denied to user 'qiao'@'localhost' for table 'sb'

授权数据库用户admin1,允许其从本机连接到mysql服务器,对auth库中的所有表有完全权限,使用密码“123456”进行验证。

mysql> grant all on auth.* to admin1@'localhost' identified by '123456';

授权数据库用户admin2,允许其从网段192.168.1.0/24种访问mysql服务器,可以查询auth库中的所有表,使用密码“123456”进行验证。

mysql> grant select on auth.* to admin2@'192.168.1.0/24' identified by '123456';

授权数据库用户admin3,允许其从bent.com域内的任何主机访问mysql服务器,对auth库下的所有表具有select,insert权限,使用密码“123456”进行验证

mysql> grant select,insert on auth.* to admin3@'%.benet.com' identified by '123456';

查看权限

需要查看用户的权限时,可直接使用select语句对user, db,host, 数据表查询也使用show语句进行查看,后者相对更加简单,命令格式为:

show grants for 用户名@域名或ip

查看数据库用户root从服务器本机进行连接时的权限

mysql> show grants for root@'localhost';

+----------------------------------------------------------------------------------------------------------------------------------------+

| grants for root@localhost                                                                                                              |

| grant all privileges on *.* to 'root'@'localhost' identified by password '*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9' with grant option |

查看数据库用户admin3从benet.com域内的客户机访问mysql服务时的权限

mysql> show grants for admin3@'%.benet.com';

+-----------------------------------------------------------------------------------------------------------------+

| grants for admin3@%.benet.com                                                                                   |

| grant usage on *.* to 'admin3'@'%.benet.com' identified by password '*6bb4837eb74329105ee4568dda7dc67ed2ca2ad9' |

| grant select, insert on `auth`.* to 'admin3'@'%.benet.com'                                                      |

2 rows in set (0.02 sec)

撤销权限

revoke语句:用于撤销指定数据库用户的权限。命令格式为:

revoke 权限列表 on 数据库名.表名 from 用户名@域名或ip

 撤销数据库用户admin3从benet.com域访问数据库auth的所有权限

mysql> revoke all on auth.* from admin3@'%.benet.com';

php编译安装

[root@localhost ~]# tar jxvf php-5.2.6.tar.bz2 -c /usr/src/

[root@localhost ~]# cd /usr/src/php-5.2.6/

[root@localhost php-5.2.6]# ./configure --prefix=/usr/local/php5 --enable-mbstring --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5

 在上述配置命令中,使用了五个命令,其含义如下

--prefix:设置php程序的安装路径

--enable-mbstring:设置php支持多字节字符串

--with-apxs2:设置apache服务器提供的apxs模块支持程序的文件位置

--with-mysql:设置mysql服务器程序的安装位置

--with-config-file-path:设置php程序的配置文件所在的位置

generating files

updating cache ./config.cache

creating ./config.status

creating php5.spec

creating main/build-defs.h

creating scripts/phpize

creating scripts/man1/phpize.1

creating scripts/php-config

creating scripts/man1/php-config.1

creating sapi/cli/php.1

creating main/php_config.h

creating main/internal_functions.c

creating main/internal_functions_cli.c

+--------------------------------------------------------------------+

| license:                                                           |

| this software is subject to the php license, available in this     |

| distribution in the file license.  by continuing this installation |

| process, you are bound by the terms of this license agreement.     |

| if you do not agree with the terms of this license, you must abort |

| the installation process at this point.                            |

thank you for using php.

[root@localhost php-5.2.6]# make

ts.lo zend/zend_object_handlers.lo zend/zend_objects_api.lo zend/zend_default_classes.lo zend/zend_execute.lo sapi/cli/php_cli.lo sapi/cli/php_cli_readline.lo sapi/cli/getopt.lo main/internal_functions_cli.lo -lcrypt -lcrypt -lrt -lmysqlclient -lresolv -lm -ldl -lnsl -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt -lxml2 -lz -lm -lxml2 -lz -lm -lxml2 -lz -lm -lcrypt  -o sapi/cli/php

build complete.

don't forget to run 'make test'.

[root@localhost php-5.2.6]# make install

[activating module `php5' in /usr/local/apache2/conf/httpd.conf]

installing php cli binary:        /usr/local/php5/bin/

installing php cli man page:      /usr/local/php5/man/man1/

installing build environment:     /usr/local/php5/lib/php/build/

installing header files:          /usr/local/php5/include/php/

installing helper programs:       /usr/local/php5/bin/

  program: phpize

  program: php-config

installing man pages:             /usr/local/php5/man/man1/

  page: phpize.1

  page: php-config.1

installing pear environment:      /usr/local/php5/lib/php/

[pear] console_getopt - installed: 1.2.3

[pear] archive_tar    - installed: 1.3.2

[pear] structures_graph- installed: 1.0.2

pear/pear can optionally use package "pear/xml_rpc" (version >= 1.4.0)

[pear] pear           - installed: 1.7.1

wrote pear system config file at: /usr/local/php5/etc/pear.conf

you may want to add: /usr/local/php5/lib/php to your php.ini include_path

installing pdo headers:          /usr/local/php5/include/php/ext/pdo/

 复制php.ini配置文件

php安装程序通常不会复制配置文件,因此还需要复制源码目录中的php配置模板文件php.ini-dist,将其复制为php配置目录中的php.ini文件即可

[root@localhost php-5.2.6]# cp php.ini-dist /usr/local/php5/php.ini

[root@localhost php-5.2.6]# cd

 设置httpd.conf文件

php程序需要和web服务器协同工作,才便发挥其强大的作用。因此在编译安装php程序以后,还要编辑httpd服务器的主配置文件httpd.conf,添加支持php环境的相关配置。

 要让httpd服务器支持php页面解析功能,可以使用“loadmodule php5_modulemodules/libphp5.so”,如果没有侧手动添加

 还需要使用“addtpe”配置项添加对.php类型的网页的识别。在httpd.conf文件中查找“addtype”关键字,参考相关配置添加配置行“addtype application/x-httpd-php.php”。

 编辑httpd.conf文件,确认或添加支持php环境的相关配置行

[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf

//使用:“set nu”命令即可显示行号

……………….

51 # example:

 52 # loadmodule foo_module modules/mod_foo.so

 53 loadmodule php5_module        modules/libphp5.so  //确定有这一行

 54 #

 55   //其中“php5_modile”是模块名称,“modules/libphp5.so”是模块文件的位置。

165 #

166 <ifmodule dir_module>

167     directoryindex index.php index.html  //修改这行配置,首页文件

168 </ifmodule>

169  //修改“directoryindex”配行,添加“index.php”项,以便httpd服务器能够正确搜索该首页

307     #

308     addtype application/x-compress .z

309     addtype application/x-gzip .gz .tgz

310     addtype application/x-httpd-php .php  //增加这一行,支持php文件识别

[root@localhost ~]# service httpd restart   //重新启动httpd服务

测试php与apache的协同工作

 编写一个php测试网页文件,使用“phpinfo()”函数显示服务器的php环境信息

[root@localhost ~]# vim /usr/local/apache2/htdocs/index.php

<?php

phpinfo();

?>

测试php与mysql数据库的协同工作,编写一个php测试网页文件,添加简单的数据库操作命令 ,用于验证mysql服务器的链接,查询操作,如果测试mysql错误会显示“fail !!”

[root@localhost ~]# vim /usr/local/apache2/htdocs/testdb.php

$link=mysql_connect('localhost','test','');

if(!$link) echo "fail !!";

else echo "success !!";

mysql_close();

架设discuz!论坛

首先建立用于bbs论坛的数据库(bbsdb)及用户(runbbs)

your mysql connection id is 2

mysql> create database bbsdb;

mysql> grant all on bbsdb.* to runbbs@localhost identified by 'qq123.com';

mysql> quit

解压软件包,上传文件

 使用unzip命令将下载的两个软件包进行解压,将释放出的“upload”目录转移至web站点的网页文档目录中,并分别改名为bbs,ucenter

[root@localhost ~]# unzip discuz_6.1.0_sc_utf8.zip -d discuz

[root@localhost ~]# mv discuz/upload/ /usr/local/apache2/htdocs/bbs

[root@localhost ~]# unzip ucenter_1.0.0_sc_utf8.zip -d ucenter

[root@localhost ~]# mv ucenter/upload/ /usr/local/apache2/htdocs/ucenter

设置相关目录及文件的属主,以便httpd服务能够对论坛写入数据

[root@localhost ~]# cd /usr/local/apache2/htdocs/ucenter/

[root@localhost ucenter]# chown -r daemon ./data/

[root@localhost ucenter]# cd ../bbs/

[root@localhost bbs]# chown -r daemon config.inc.php p_w_uploads/ forumdata/     uc_client/data/cache/

安装ucenter用户中心程序

在浏览器中访问“http://www.benet.com/ucenter/install/index.php”,根据向导页面的提示进行ucenter的安装,期间注意设置正确的数据库名,用户名,密码等基本信息,并设置好创始用户(ucenter administrator)的密码

确认目录权限设置结果正确

设置数据库名,用户, 密码等信息

设置创始人用户名及密码

进行数据库初始化,并完成安装

成功安装及登陆

登陆界面

安装discuz!论坛程序,在浏览器中访问http://www.benet.com/bbs/install/index.php根据向导页面提示进行discuz!论坛的安装

输入创始人密码,设置论坛名称等信息

设置基本信息,设置数据库信息及论坛管理员账号,密码

初始化数据库后,自动跳转至安装完成页面

关于discuz!论坛的详细安装步骤,可以参考软件包内的“users_guard.html”

使用discuz!论坛首页

url地址http://www.benet.com/bbs/index.php进入该页面

 转移“install”安装目录

在discuz!论坛安装完毕后,应及时转移bbs和ucenter目录中的install子目录,以减少被重新安装的风险。

[root@localhost ~]# cd /usr/local/apache2/htdocs/bbs/

[root@localhost bbs]# mv install/ install.lock

[root@localhost bbs]# chmod o+rx install.lock/

[root@localhost bbs]# cd ../ucenter/

[root@localhost ucenter]# mv install/ install.lok

[root@localhost ucenter]# chmod o-rx install.lok/

在浏览器中访问http://www.benet.com//bbs/admincp.php,进入discuz!论坛的管理中心。使用安装过程中设置的论坛管理员账号,密码进行登陆,即可进入到论坛的后台管理页面

安装phpmyadmin是一个使用php语言编写,通过web界面对mysql数据库进行管理的工具,使用phpmyadmin套件,即便对mysql语句不太熟悉的人,也可以在浏览器中对mysql数据库进行管理和维护操作。

 解压并释放phpmyadmin文件包,改名后转移至web站点的网页文档目录

[root@localhost ~]# tar zxvf phpmyadmin-2.11.9.5-all-languages.tar.gz

[root@localhost ~]# mv phpmyadmin-2.11.9.5-all-languages phpmyadmin

[root@localhost ~]# mv phpmyadmin /usr/local/apache2/htdocs/

[root@localhost phpmyadmin]# cp config.sample.inc.php config.inc.php //复制配置样例

[root@localhost phpmyadmin]# vim config.inc.php

……//省略配置显示

$cfg['blowfish_secret'] = 'asd123';  //查找此行,设置密码

若“blowfish_secret”行的密码为空,则在访问phpmyadmin登陆页面时会报错“错误:配置文件现在需要绝密的短密码(blowfsh_secret)。

 在浏览器中访问http://www.benet.com//phpmyadmin/index.php,使用mysql数据库账号进行登陆

在phpmyadmin的web页面下方,可能会出现“无法载入mcrtpt拓展,请检查php配置”的错误提示信息,这是由于没有mcrypt加密算法扩展库造成的,缺少mcrypt库并不会影响phpmyadmin的基本使用,可以忽略提示。

mysql命令

修改管理员的密码和用户登录:

[root@localhost ~]# mysqladmin -u root password "lyaccp" // 设置密码

[root@localhost ~]# mysqladmin -u root -p password "lybenet" // 修改密码

[root@localhost ~]# mysql -u root -p // 登录

[root@localhost ~]# mysql -uroot -plybenet // 登录

[root@localhost ~]# mysql --user=root --password=lybenet // 登录

[root@localhost ~]# mysql --user=root --password=lybenet

welcome to the mysql monitor. commands end with ; or \g.

查看数据库:

// 查看当前选择的数据库:

mysql> select database();

// 查看当前登录的数据库用户:

mysql> select user();

// 查看数据库

[root@localhost ~]# mysqlshow -uroot -p123456 // 用户:root 密码:123456

// 登录后的查看数据库和表

mysql> showtables from auth; // 查看数据库auth中的表信息,不需切换数据库

mysql> use mysql; // 切换到数据库mysql

mysql> showtables; // 查看数据mysql中的数据表信息

mysql> describe user; // 显示数据表user 的结构

创建数据库和表(表名users 和后面括号可以有空格)

mysql> create table users(user_name char(30) not null, user_passwd char(20)

not null default '123456', primary key (user_name));

query ok, 0 rows affected (0.04 sec)

// 不需切换数据库,创建表users1

mysql> create table auth.users1(user_name char(30) not null, user_passwd

char(20) not null default '123456', primary key(user_name));

删除表和数据库:

插入数据(表名users和后面括号可以有空格)

// 插入用户张三,密码123,使用encrypt函数加密,不需切换数据库

mysql> insert into users(user_name,user_passwd) values('zhangsan',encrypt('123'));

mysql> insert into auth.users(user_name,user_passwd) values('lisi',encrypt('123'));

查看数据:

mysql> select user_name,user_passwd from auth.userswhere user_name='zhangsan';

修改数据:

// 修改lisi的密码为456,并且encrypt函数加密

mysql> update auth.users set user_passwd=encrypt('456') where user_name='lisi';

// 修改root管理员的密码,使用“flush privileges”语句刷新用户授权信息

mysql> update mysql.user set password=password('123456')where user='root';

query ok, 3 rows affected (0.04 sec)

rows matched: 3 changed: 3 warnings: 0

删除数据:

mysql> delete from auth.userswhere user_name='zhangsan';

// 从表users中删除所有信息

mysql> delete from auth.users;

数据库备份和还原:

// 备份数据库auth

// 还原数据库auth(需指定目标数据库auth,如果备份文件有多个数据库,不需指定)

// 备份数据库auth中的表users、users1和还原表(必需指定目标数据库)

[root@localhost ~]# mysqldump -u root -p auth users users1 > mysql-auth.users.sql

[root@localhost ~]# mysql -u root -p auth < mysql-auth.users.sql

// 备份所有数据库和还原

用户及权限设置:

// 授权数据库用户tom从本机访问mysql服务器

mysql> grant all on auth.* to tom@'localhost' identified by '123';

// 授权数据库用户john从10.0.0.0/8 网络中连接到mysql服务器,对auth数据库中所有表

拥有完全权限

mysql> grant all on auth.* to john@'10.0.0.0/8' identified by '123';

// 查看当前用户的权限:

mysql> show grants;

// 查看tom用户从本机连接是的权限

mysql> show grants for tom@localhost;

// 撤销用户tom从本机访问数据库auth的所有权限

mysql> revoke all on auth.* from tom@'localhost';

// 查看数据库auth中所有授权的用户

mysql> select host,user,db from mysql.dbwhere db='auth';

继续阅读