php 在 MySQL Linux 服务器上安装 PDO 驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13375061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing PDO driver on MySQL Linux server
提问by Yuri Scarbaci
I was suggested, not long ago, to change my code to use PDO in order to parameterize my queries and safely save HTML in the database.
不久前,有人建议我更改我的代码以使用 PDO,以便参数化我的查询并安全地将 HTML 保存在数据库中。
Well, here are the main problems:
嗯,这里是主要问题:
I looked at http://php.net/manual/en/ref.pdo-mysql.php, and I don't really get where I should put that
$ ./configure --with-pdo-mysqlstring...The site I'm building actually only requires PDO for one page. While I may consider re-writing it, it would take a while and I need the pages to be running soon, so I can't turn off MySQL completely. If I do install PDO, will I still be able to use
mysql_*handlers?
我查看了http://php.net/manual/en/ref.pdo-mysql.php,我真的不知道我应该把那个
$ ./configure --with-pdo-mysql字符串放在哪里......我正在构建的站点实际上只需要一页的 PDO。虽然我可能会考虑重新编写它,但这需要一段时间并且我需要页面尽快运行,所以我不能完全关闭 MySQL。如果我确实安装了 PDO,我还能使用
mysql_*处理程序吗?
The server in question is running PHP Version 5.4.6-1ubuntu1 and Apache/2.2.22 (Ubuntu). I'm also running a phpMyAdmin database, if it matters.
有问题的服务器正在运行 PHP 版本 5.4.6-1ubuntu1 和 Apache/2.2.22 (Ubuntu)。如果重要的话,我也在运行一个 phpMyAdmin 数据库。
回答by Jani Hartikainen
On Ubuntu you should be able to install the necessary PDO parts from apt using sudo apt-get install php5-mysql
在 Ubuntu 上,您应该能够使用 apt 安装必要的 PDO 部件 sudo apt-get install php5-mysql
There is no limitation between using PDO and mysql_ simultaneously. You will however need to create two connections to your DB, one with mysql_ and one using PDO.
同时使用 PDO 和 mysql_ 之间没有限制。但是,您需要创建两个到数据库的连接,一个使用 mysql_,另一个使用 PDO。
回答by artragis
That's a good question, but I think you just misunderstand what you read.
这是一个很好的问题,但我认为你只是误解了你读到的内容。
Install PDO
安装 PDO
The ./config --with-pdo-mysqlis something you have to put on onlyif you compile your own PHP code. If you install it with package managers, you just have to use the command line given by Jany Hartikainen: sudo apt-get install php5-mysqland also sudo apt-get install pdo-mysql
该./config --with-pdo-mysql是你必须把东西只有当你编译自己的PHP代码。如果用包管理器安装它,你只需要使用由Jany Hartikainen给出的命令行:sudo apt-get install php5-mysql还sudo apt-get install pdo-mysql
Compatibility with mysql_
与 mysql_ 的兼容性
Apart from the fact mysql_ is really discouraged, they are both independent. If you use PDO mysql_ is not implicated, and if you use mysql_ PDO is not required.
除了真的不鼓励mysql_之外,它们都是独立的。如果你使用 PDO mysql_ 没有牵连,如果你使用 mysql_ PDO 是不需要的。
If you turn off PDO without changing any line in your code, you won't have a problem. But since you started to connect and write queries with PDO, you have to keep it and give up mysql_.
如果您在不更改代码中的任何行的情况下关闭 PDO,则不会有问题。但是既然你开始用 PDO 连接和写查询,你就必须保留它并放弃 mysql_。
Several years ago the MySQL team published a script to migrate to MySQLi. I don't know if it can be customised, but it's official.
几年前,MySQL 团队发布了一个迁移到 MySQLi的脚本。我不知道它是否可以定制,但它是官方的。
回答by Tobias Gaertner
Basically the answer from Jani Hartikainen is right! I upvoted his answer. What was missing on my system (based on Ubuntu 15.04) was to enable PDO Extension in my php.ini
基本上 Jani Hartikainen 的答案是正确的!我赞成他的回答。我的系统(基于 Ubuntu 15.04)缺少的是在我的 php.ini 中启用 PDO 扩展
extension=pdo.so
extension=pdo_mysql.so
restart the webserver (e.g. with "sudo service apache2 restart") -> every fine :-)
重新启动网络服务器(例如使用“sudo service apache2 restart”)-> 一切正常:-)
To find where your current active php.ini file is located you can use phpinfo() or some other hints from here: https://www.ostraining.com/blog/coding/phpini-file/
要查找当前活动的 php.ini 文件所在的位置,您可以使用 phpinfo() 或此处的其他一些提示:https: //www.ostraining.com/blog/coding/phpini-file/
回答by Avnish alok
At first install necessary PDO parts by running the command sudo apt-get install php*-mysqlwhere * is a version name of php like 5.6, 7.0, 7.1, 7.2 After installation you need to mention(uncomment if is already there) these two statements
首先通过运行命令来安装必要的 PDO 部分,sudo apt-get install php*-mysql其中 * 是 php 的版本名称,如 5.6、7.0、7.1、7.2 安装后需要提及(如果已经存在则取消注释)这两条语句
extension=pdo.so
extension=pdo_mysql.so
in your .ini file and restart your server by command
在您的 .ini 文件中并通过命令重新启动您的服务器
sudo service apache2 restart
回答by Elaine Ossipov
- PDO stands for PHP Data Object.
- PDO_MYSQL is the driver that will implement the interface between the dataobject(database) and the user input (a layer under the user interface called "code behind") accessing your data object, the MySQL database.
- PDO 代表 PHP 数据对象。
- PDO_MYSQL 是实现数据对象(数据库)和访问数据对象 MySQL 数据库的用户输入(用户界面下的一层称为“代码隐藏”)之间的接口的驱动程序。
The purpose of using this is to implement an additional layer of security between the user interface and the database. By using this layer, data can be normalized before being inserted into your data structure. (Capitals are Capitals, no leading or trailing spaces, all dates at properly formed.)
使用它的目的是在用户界面和数据库之间实现额外的安全层。通过使用该层,可以在将数据插入数据结构之前对其进行规范化。(大写是大写,没有前导或尾随空格,所有日期格式正确。)
But there are a few nuances to this which you might not be aware of.
但是,您可能没有意识到这其中的一些细微差别。
First of all, up until now, you've probably written all your queries in something similar to the URL, and you pass the parameters using the URL itself. Using the PDO, all of this is done under the user interface level. User interface hands off the ball to the PDO which carries it down field and plants it into the database for a 7-point TOUCHDOWN.. he gets seven points, because he got it there and did so much more securely than passing information through the URL.
首先,到目前为止,您可能已经在类似于 URL 的内容中编写了所有查询,并使用 URL 本身传递参数。使用 PDO,所有这些都在用户界面级别下完成。用户界面将球交给 PDO,PDO 将球传给场地并将其植入数据库进行 7 点 TOUCHDOWN.. 他得到 7 分,因为他做到了,而且比通过 URL 传递信息安全得多.
You can also harden your site to SQL injectionby using a data-layer. By using this intermediary layer that is the ONLY 'player' who talks to the database itself, I'm sure you can see how this could be much more secure. Interface to datalayer to database, datalayer to database to datalayer to interface.
您还可以使用数据层来强化您的站点以进行SQL 注入。通过使用这个作为唯一与数据库本身对话的“参与者”的中间层,我相信您可以看到这如何更加安全。接口到数据层到数据库,数据层到数据库到数据层到接口。
And:
和:
By implementing best practices while writing your code you will be much happier with the outcome.
通过在编写代码时实施最佳实践,您会对结果更加满意。
Additional sources:
其他来源:
Re: MySQL Functions in the url php dot net/manual/en/ref dot pdo-mysql dot php
回复:URL 中的 MySQL 函数 php dot net/manual/en/ref dot pdo-mysql dot php
Re: three-tier architecture - adding security to your applications https://blog.42.nl/articles/introducing-a-security-layer-in-your-application-architecture/
回复:三层架构 - 为您的应用程序添加安全性 https://blog.42.nl/articles/introducing-a-security-layer-in-your-application-architecture/
Re: Object Oriented Design using UML If you really want to learn more about this, this is the best book on the market, Grady Booch was the father of UML http://dl.acm.org/citation.cfm?id=291167&CFID=241218549&CFTOKEN=82813028
回复:使用 UML 的面向对象设计如果你真的想了解更多关于这方面的知识,这是市场上最好的书,Grady Booch 是 UML 之父 http://dl.acm.org/citation.cfm?id=291167&CFID =241218549&CFTOKEN=82813028
Or check with bitmonkey. There's a group there I'm sure you could learn a lot with.
或者用bitmonkey检查。那里有一个小组,我相信你可以在那里学到很多东西。
>
>
If we knew what the terminology really meant we wouldn't need to learn anything.
如果我们知道术语的真正含义,我们就不需要学习任何东西。
>
>
回答by Marcello de Sales
If you need a CakePHP Docker Container with MySQL, I have created a Docker image for that purpose! No need to worry about setting it up. It just works!
如果你需要一个带有 MySQL 的 CakePHP Docker 容器,我已经为此创建了一个 Docker 镜像!无需担心设置。它只是有效!
- GitHub: https://github.com/marcellodesales/php-apache-mysql-4-cakephp-docker
- DockerHub: https://hub.docker.com/r/marcellodesales/php-apache-cakephp-mysql/
- GitHub: https://github.com/marcellodesales/php-apache-mysql-4-cakephp-docker
- DockerHub:https://hub.docker.com/r/marcellodesales/php-apache-cakephp-mysql/
Here's how I installed in Ubuntu-based image:
以下是我在基于 Ubuntu 的映像中的安装方式:
https://github.com/marcellodesales/php-apache-mysql-4-cakephp-docker/blob/master/Dockerfile#L8
https://github.com/marcellodesales/php-apache-mysql-4-cakephp-docker/blob/master/Dockerfile#L8
RUN docker-php-ext-install mysql mysqli pdo pdo_mysql
Building and running your application is just a 2 step process (considering you are in the current directory of the app):
构建和运行您的应用程序只是一个 2 步过程(考虑到您在应用程序的当前目录中):
$ docker build -t myCakePhpApp .
$ docker run -ti myCakePhpApp

