一、搭建LAMP
具体步骤:
1.首先为了搭建一个稳定的lamp的练习环境,确保你的虚拟机可以连网,这里我们使用的yum安装,它可以帮助我们解决软件自己的依赖关系.我还在后面加了postgresql数据库如果不需要的话可以去掉和postgresql的参数.命令如下
yum -y install httpd mysql mysql-server php php-mysql postgresql postgresql-server php-postgresql php-pgsql php-devel
2.启动apache服务并查看时候启动成功
命令如下:(切记用root用户启动服务)
启动:/etc/rc.d/init.d/httpd start
检测启动结果:ps aux | grep httpd
3.进入编辑:cd /etc/var/www/html
4.如果上述进展顺利,那我们接下来进行mysql数据库的配置
命令如下:
启动: /etc/rc.d/init.d/mysqld start
检查启动结果: netstat -tulnp | grep :3306
修改root密码: mysqladmin -u root password ‘你想设置的密码’
5.进入mysql命令行:
登陆:mysql -u root -p
密码:123456(上面修改的密码)
6.进入mysql数据库,创建demo库和用于测试的person表,并插入测试数据
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.04 sec)
mysql> create databases demo;
mysql> alter table person add name varchar(255);
Query OK, 0 rows affected (0.00 sec)
mysql> desc person;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> alter table person add del_flg int(2) default 0;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc person;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| del_flg | int(2) | YES | | 0 | |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into person (id,name,del_flg) value (null,'yk',1);
Query OK, 1 row affected (0.00 sec)
mysql> desc person;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| del_flg | int(2) | YES | | 0 | |
+---------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into person (id,name,del_flg) values (null,'lkp',2);
Query OK, 1 row affected (0.00 sec)
mysql> insert into person (id,name,del_flg) values (null,'kyle',3);
Query OK, 1 row affected (0.00 sec)
mysql> exit;
Bye
7.最后编写php连接mysql的测试代码,检测mysql是否能正常配合php工作
<?php
header("Content-type:text/html;charset=utf-8");
$link = mysql_connect('localhost','root','123456');
mysql_select_db('demo',$link);
mysql_query('set names utf-8');
$query_sql = "select * from person";
$result = mysql_query($query_sql);
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
echo "<pre>";
print_r($data);
详细的搭建方法参考这里
问题一:[root@root ~]# 如何改成 [root@localhost ~]
首先要知道:
[root@localhost ~]# cd /
[root@localhost /]$
root代表用户名
localhost 主机名
~ 用户的家目录
/ 根目录
.# 表示超级管理员在操作
$ 普通用户在操作
修改方法:
在/etc/sysconfig/network里改掉HOSTNAME即可
问题二: Host '*****' is not allowed to connect to this MySQL server
详细解决方法在这里 点我
问题三:php和mysql配合使用测试
header("Content-type:text/html;charset=utf-8");
$link = mysql_connect('localhost','root','root');
mysql_select_db('demo',$link);
mysql_query('set names utf-8);
$query_sql = "select * from person";
$result = mysql_query($query_sql);
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[] = $row;
}
echo "<pre>";
print_r($data);
显示如下图说明成功