win7+php7+laravel5.3+nginx+oracle12c支持 全教程
扫描二维码
随时随地手机看文章
首先win7 环境是必须的,这里就不说如何安装OS了,请自行百度
1. 安装 phpStudy,这是我第一次听说这个软件,看起来真的不错,可以自行选择版本将运行环境进行打包处理,这里直接选择php 7.0.12-nts+nginx即可
2. 开始安装composer ,下载地址是 https://getcomposer.org/Composer-Setup.exe,一路确定即可
3.因为composer源的问题可以直接换源,执行以下命令
composer config -g repo.packagist composer https://packagist.phpcomposer.com
4. 下载laravel5.3 版本,下载地址是:https://github.com/laravel/laravel 并将其加压缩到你需要的位置上即可,我是 D:\runninglaravel
5. 进行composer安装 ,使用cmd 进入到 D:\runninglaravel 目录下,执行命令:
composer install
6. 复制.env.example 为.env文件,执行命令
copy .env.example .env
7. 执行命令:
php artisan key:generate
8. 接下来是进行php study的修改,将站点目录修改为 D:\runninglarave\public
9. 修改nginx.conf 其目的是将所有的请求定位到publicindex.php文件上进行统一处理
location / { root "D:/running/laravel/public"; index index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
这是我的处理文件,很糙,但是目前可用
10. 到这里为止,再检查一下php的扩展 需要打开的是open_ssl (这个我遇到过问题,即使打开了也会报错 encry**函数未定义,我的处理方式是重启电脑)
11. laravel已经安装好,现在需要做的是进行laravel的oracle支持,laravel默认是不支持oracle的,现在添加第三方的组件
composer require yajra/laravel-oci8
12. 如果要用php连接oracle不光需要php扩展,还需要oracle的客户端程序,
下载文件:http://download.oracle.com/otn/nt/instantclient/121020/instantclient-basic-nt-12.1.0.2.0.zip?AuthParam=1488258321_c18d9fdc85acf4d32185f80bcd7e3b5a
同时还需要将文件解压缩后,并将解压缩位置添加在环境变量上
13. 开启php 扩展, 打开php_oci php_oci_11g php_pdo_oci,需要注意的还有就是打开
oci8.privileged_connect = On
允许oci_connect连接,这样就同时支持了两种方式一个是pdo方式,第二个是开启oci_connnect连接方式
14. 虽然框架中已经有了对oracle的支持,但是在接入点还需要进行修改
在 IlluminateDatabaseConnectorsConnectionFactory.php ; 添加两个类的声明
use YajraOci8ConnectorsOracleConnector; use YajraOci8Oci8Connection;
还是这个文件还需要重写两个函数
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = []) { if ($resolver = Connection::getResolver($driver)) { return $resolver($connection, $database, $prefix, $config); } switch ($driver) { case 'mysql': return new MySqlConnection($connection, $database, $prefix, $config); case 'pgsql': return new PostgresConnection($connection, $database, $prefix, $config); case 'sqlite': return new SQLiteConnection($connection, $database, $prefix, $config); case 'sqlsrv': return new SqlServerConnection($connection, $database, $prefix, $config); case 'oracle': return new Oci8Connection($connection, $database, $prefix, $config);; } throw new InvalidArgumentException("Unsupported driver [$driver]"); } } public function createConnector(array $config) { if (! isset($config['driver'])) { throw new InvalidArgumentException('A driver must be specified.'); } if ($this->container->bound($key = "db.connector.{$config['driver']}")) { return $this->container->make($key); } switch ($config['driver']) { case 'mysql': return new MySqlConnector; case 'pgsql': return new PostgresConnector; case 'sqlite': return new SQLiteConnector; case 'sqlsrv': return new SqlServerConnector; case 'oracle': return new OracleConnector; } throw new InvalidArgumentException("Unsupported driver [{$config['driver']}]"); }
最后就是修改config中database.php文件中的配置了,
'oracle' => [ 'driver' => 'oracle', 'tns' => env('DB_TNS', ''), 'host' => '192.168.11.111', 'port' => 1521, 'database' => 'ORCL', 'username' => "c##test1", 'password' => '111111', 'charset' => env('DB_CHARSET', 'AL32UTF8'), 'prefix' => env('DB_PREFIX', ''), 'prefix_schema' => env('DB_SCHEMA_PREFIX', ''), ],
同时修改default
'default' => 'oracle',
至于用户名是C##开始的,请自行百度,关于CDB和PDB的区别
测试代码:
Route::get('test', function () { $m = DB::table("users_data")->get(); var_dump($m->all()); });
PS: 现在附上PDO方式的连接测试代码
$tns = " (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.11.111)(PORT = 1521)) ) (CONNECT_DATA = (Service_Name = orcl) ) ) "; $db_username = "C##test1"; #这里不能使用 sys角色的用户 $db_password = "111111"; try { $conn = new PDO("oci:dbname=" . $tns, $db_username, $db_password); } catch (PDOException $e) { echo($e->getMessage()); } $sql = "select * from users_data"; $rs = $conn->query($sql); $m = $rs->fetchAll();
oci_connect连接的测试代码
$conn = oci_connect('C##test1', '111111', '192.168.11.111/orcl'); if (!$conn) { $e = oci_error(); print htmlentities($e['message']); exit; }else { echo "连接oracle成功!"; }
最后关于 laravel框架连接oracle的方式是使用oci_connect的方式进行连接
public function createConnection($tns, array $config, array $options) { // add fallback in case driver is not set, will use pdo instead if (! in_array($config['driver'], ['oci8', 'pdo-via-oci8', 'oracle'])) { return parent::createConnection($tns, $config, $options); } $config = $this->setCharset($config); $options['charset'] = $config['charset']; return new Oci8($tns, $config['username'], $config['password'], $options); }
如果当初并未设定oracle或者oci8 或者pdo-via-oci8的话,则使用PDO方式,否则使用oci8的oci_connect方式进行连接
OK: 我的整个搭建过程到此为止,如有问题敬请留言!