首页 > 编程技术 > php

PHP安装mysql.so扩展的方法介绍

发布时间:2017-1-22 11:08

这篇文章介绍了PHP安装mysql.so扩展的方法,非常实用,有兴趣的同学可以参考一下

因为在PHP中mysql_connect模块已经逐渐被弃用,我在搭建环境时也没有再安装mysql扩展,但是今天在维护一个老项目时,出现报错

Fatal error: Uncaught Error: Call to undefined function mysql_connect()

于是google一下,发现如果php和mysql都已经安装完成了,可以使用phpize工具手动编译生成mysql.so扩展来解决

下面是操作步骤:

1.进入php源码的ext/mysql目录

cd/usr/local/src/php-5.6.29/ext/mysql/

2.运行phpize,在该目录下生成一个configure文件(php安装目录:/usr/local/php/)

/usr/local/php/bin/phpize

3.运行configure,指明php-config文件位置(/usr/local/php/bin/php-config)和mysql安装目录(/usr/local/mysql/)

./configure--with-php-config=/usr/local/php/bin/php-config--with-mysql=/usr/local/mysql/

4.编译安装,生成mysql.so

make&&makeinstall

5.修改php.ini文件,添加mysql.so扩展配置,保存退出

extension=mysql.so

6.重启php-fpm

service php-fpm restart

7.测试,在web目录下添加php文件,如/usr/local/nginx/html/mysql.php

<?php
$con= mysql_connect('localhost','root','');
if($con){
 die('ok');
}else{
 die('Could not connect: '. mysql_error());
}

访问URL,如:http://192.168.8.9/mysql.php

显示ok,则配置成功

最近有同事问:php mysql如何实现mysql_select_db选择数据库?小编分享了这篇文章解答了问题,在PHP中,与MySQL服务器建立连接后,需要确定所要连接的数据库,此时我们可以使用mysql_select_db函数,该函数用于选择需要操作的数据库。有兴趣的同学参考一下吧。

mysql_select_db介绍

mysql_select_db函数有两个参数:

 代码如下 复制代码
mysql_select_db(database,connection)

如果成功,则该函数返回 true。如果失败,则返回 false

参数 描述
database 必需。需要选择的数据库。
connection 可选。MySQL 连接。如果没有设置该参数,则表明使用上一个mysql数据库连接。

 mysql_select_db实例

 
 代码如下 复制代码
<?php
 $host="mysql153.secureserver.net";
 $uname="root";
 $pass="password";
  
 $connection= mysql_connect ($host,$uname,$pass);
  
 if(!$connection) {
   die("mysql连接失败");
 }

 

 $result=mysql_select_db ("manongjc");
  
 if(!$result) {
   die("没有选择任何数据库");
 }
    
 echo"数据库manongjc已经选择,可以使用该数据库了";
?>
这篇文章写了php mysql操作mysql_connect连接数据库的教程,php操作数据库首先必须连接到指定的数据库,连接数据库可以使用PHP mysql_connect函数。有兴趣的同学可以参考一下!

mysql_connect介绍

php mysql_connect用于连接mysql服务器,该函数有多个参数,但我们一般只需要了解以下三个参数即可:

mysql_connect(server,user,pwd)

参数介绍:

如果连接成功,则返回一个 MySQL 连接标识,如果连接失败则返回 FALSE。

 mysql_connect实例

<?php
$host="mysql153.secureserver.net";
$uname="root";
$pass="password";
 
$connection= mysql_connect ($host,$uname,$pass);
if(!$connection) {
 die("mysql服务器连接失败");
}else{
 echo"恭喜你!mysql服务器连接成功";
}
?>
Redis数据库现在越来越流行了我们基本可以使用Redis代替了mysql数据库了,下面我们来看PHP 中使用 Redis数据库的例子吧。


安装

开始在 PHP 中使用 Redis 前, 我们需要确保已经安装了 redis 服务及 PHP redis 驱动,且你的机器上能正常使用 PHP。 接下来让我们安装 PHP redis 驱动:下载地址为: https://github.com/phpredis/phpredis/releases 。

PHP安装 redis 扩展

以下操作需要在下载的 phpredis 目录中完成:

$ wget https://github.com/phpredis/phpredis/archive/2.2.4.tar.gz
$ cd phpredis-2.2.7                      # 进入 phpredis 目录
$ /usr/local/php/bin/phpize              # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install
如果你是 PHP7 版本,则需要下载指定分支:

git clone -b php7 https://github.com/phpredis/phpredis.git
修改php.ini文件

vi /usr/local/php/lib/php.ini
增加如下内容:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"

extension=redis.so
安装完成后重启php-fpm 或 apache。查看phpinfo信息,就能看到redis扩展。


连接到 redis 服务

<?php
    //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
         //查看服务是否运行
   echo "Server is running: " . $redis->ping();
?>
执行脚本,输出结果为:

Connection to server sucessfully
Server is running: PONG
Redis PHP String(字符串) 实例

<?php
   //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //设置 redis 字符串数据
   $redis->set("tutorial-name", "Redis tutorial");
   // 获取存储的数据并输出
   echo "Stored string in redis:: " . $redis->get("tutorial-name");
?>
执行脚本,输出结果为:

Connection to server sucessfully
Stored string in redis:: Redis tutorial
Redis PHP List(列表) 实例

<?php
   //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   //存储数据到列表中
   $redis->lpush("tutorial-list", "Redis");
   $redis->lpush("tutorial-list", "Mongodb");
   $redis->lpush("tutorial-list", "Mysql");
   // 获取存储的数据并输出
   $arList = $redis->lrange("tutorial-list", 0 ,5);
   echo "Stored string in redis:: "
   print_r($arList);
?>
执行脚本,输出结果为:

Connection to server sucessfully
Stored string in redis::
Redis
Mongodb
Mysql
Redis PHP Keys 实例

<?php
   //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   echo "Connection to server sucessfully";
   // 获取数据并输出
   $arList = $redis->keys("*");
   echo "Stored keys in redis:: "
   print_r($arList);
?>
执行脚本,输出结果为:

Connection to server sucessfully
Stored string in redis::
tutorial-name
tutorial-list

标签:[!--infotagslink--]

您可能感兴趣的文章: