在本文中,我们将为您详细介绍30个优化MySQL语句的技巧的相关知识,并且为您解答关于如何优化mysql语句的疑问,此外,我们还会提供一些关于12个优化Docker镜像安全性的技巧、40个优化PHP代
在本文中,我们将为您详细介绍30 个优化 MySQL 语句的技巧的相关知识,并且为您解答关于如何优化mysql语句的疑问,此外,我们还会提供一些关于12 个优化 Docker 镜像安全性的技巧、40 个优化 PHP 代码的小窍门 (英文)、Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)、Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''的有用信息。
本文目录一览:- 30 个优化 MySQL 语句的技巧(如何优化mysql语句)
- 12 个优化 Docker 镜像安全性的技巧
- 40 个优化 PHP 代码的小窍门 (英文)
- Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)
- Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''
30 个优化 MySQL 语句的技巧(如何优化mysql语句)
1、应尽量避免在 where 子句中使用 != 或 操作符,否则将引擎放弃使用索引而进行全表扫描。
2、对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。
3、应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num is null
可以在 num 上设置默认值 0,确保表中 num 列没有 null 值,然后这样查询:
select id from t where num=0
4、尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如:
select id from t where num=10 or num=20
可以这样查询:
select id from t where num=10union allselect id from t where num=20
5、下面的查询也将导致全表扫描:(不能前置百分号)
select id from t where name like '�c%'
若要提高效率,可以考虑全文检索。
6、in 和 not in 也要慎用,否则会导致全表扫描,如:
select id from t where num in(1,2,3)
对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3
7、如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然 而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num
可以改为强制查询使用索引:
select id from t with(index(索引名)) where num=@num
8、应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描。如:
select id from t where num/2=100
应改为:
select id from t where num=100*2
9、应尽量避免在 where 子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。如:
select id from t where substring(name,1,3)=’abc’–name以abc开头的id select id from t where datediff(day,createdate,’2005-11-30′)=0–’2005-11-30′生成的id
应改为:
select id from t where name like ‘abc%’ select id from t where createdate>=’2005-11-30′ and createdate<’2005-12-1′
10、不要在 where 子句中的 “=” 左边进行函数、算术运算或其他表达式运算,否则系统将可能无法正确使用索引。
11、在使用索引字段作为条件时,如果该索引是复合索引,那么必须使用到该索引中的第一个字段作为条件时才能保证系统使用该索引,否则该索引将不会被使 用,并且应尽可能的让字段顺序与索引顺序相一致。
12、不要写一些没有意义的查询,如需要生成一个空表结构:
select col1,col2 into #t from t where 1=0
这类代码不会返回任何结果集,但是会消耗系统资源的,应改成这样:
create table #t(…)
13、很多时候用 exists 代替 in 是一个好的选择:
select num from a where num in(select num from b)
用下面的语句替换:
select num from a where exists(select 1 from b where num=a.num)
14、并不是所有索引对查询都有效,SQL是根据表中数据来进行查询优化的,当索引列有大量数据重复时,SQL查询可能不会去利用索引,如一表中有字段 sex,male、female 几乎各一半,那么即使在 sex 上建了索引也对查询效率起不了作用。
15、索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,所以怎样建索引需要慎重考虑,视具体情况而定。一个表的索引数最好不要超过6个,若太多则应考虑一些不常使用到的列上建的索引是否有 必要。
16.应尽可能的避免更新 clustered 索引数据列,因为 clustered 索引数据列的顺序就是表记录的物理存储顺序,一旦该列值改变将导致整个表记录的顺序的调整,会耗费相当大的资源。若应用系统需要频繁更新 clustered 索引数据列,那么需要考虑是否应将该索引建为 clustered 索引。
17、尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能,并会增加存储开销。这是因为引擎在处理查询和连接时会 逐个比较字符串中每一个字符,而对于数字型而言只需要比较一次就够了。
18、尽可能的使用 varchar/nvarchar 代替 char/nchar ,因为首先变长字段存储空间小,可以节省存储空间,其次对于查询来说,在一个相对较小的字段内搜索效率显然要高些。
19、任何地方都不要使用 select * from t ,用具体的字段列表代替“*”,不要返回用不到的任何字段。
20、尽量使用表变量来代替临时表。如果表变量包含大量数据,请注意索引非常有限(只有主键索引)。
21、避免频繁创建和删除临时表,以减少系统表资源的消耗。
22、临时表并不是不可使用,适当地使用它们可以使某些例程更有效,例如,当需要重复引用大型表或常用表中的某个数据集时。但是,对于一次性事件,最好使 用导出表。
23、在新建临时表时,如果一次性插入数据量很大,那么可以使用 select into 代替 create table,避免造成大量 log ,以提高速度;如果数据量不大,为了缓和系统表的资源,应先 create table,然后 insert。
24、如果使用到了临时表,在存储过程的最后务必将所有的临时表显式删除,先 truncate table ,然后 drop table ,这样可以避免系统表的较长时间锁定。
尽量避免使用游标,因为游标的效率较差,如果游标操作的数据超过1万行,那么就应该考虑改写。
26、使用基于游标的方法或临时表方法之前,应先寻找基于集的解决方案来解决问题,基于集的方法通常更有效。
27、与临时表一样,游标并不是不可使用。对小型数据集使用 FAST_FORWARD 游标通常要优于其他逐行处理方法,尤其是在必须引用几个表才能获得所需的数据时。在结果集中包括『合计』 的例程通常要比使用游标执行的速度快。如果开发时 间允许,基于游标的方法和基于集的方法都可以尝试一下,看哪一种方法的效果更好。
28、在所有的存储过程和触发器的开始处设置 SET NOCOUNT ON ,在结束时设置 SET NOCOUNT OFF 。无需在执行存储过程和触发器的每个语句后向客户端发送 DONE_IN_PROC 消息。
29、尽量避免向客户端返回大数据量,若数据量过大,应该考虑相应需求是否合理。
30、尽量避免大事务操作,提高系统并发能力。
12 个优化 Docker 镜像安全性的技巧
本文介绍了 12 个优化 Docker 镜像安全性的技巧。每个技巧都解释了底层的攻击载体,以及一个或多个缓解方法。这些技巧包括了避免泄露构建密钥、以非 root 用户身份运行,或如何确保使用最新的依赖和更新等。
01 前言
当你是刚开始使用 Docker 的新手时,你很可能会创建不安全的 Docker 镜像,使攻击者很容易借此接管容器,甚至可能接管整个主机,然后渗透到你公司的其他基础设施中。
可以被滥用来接管你的系统的攻击向量有很多,例如:
-
启动的应用程序(在你 Dockerfile 的 ENTRYPOINT 中指定)以 root 用户身份运行。这样以来,一旦攻击者利用了一个漏洞并获得了 shell 权限,他们就可以接管 Docker 守护程序所运行的主机。
-
你的镜像是基于一个过时的和 / 或不安全的基础镜像,其中包含(现在)众所周知的安全漏洞。
-
你的镜像包含了一些工具(如 curl、apt 等),一旦攻击者获得了某种访问权,就可以通过这些工具将恶意软件加载到容器中。
下面的各个章节讲解了能够优化你的镜像安全性的各种方法。它们是按重要性 / 影响程度排序的,也就是说排名靠前的方法更重要。
02 避免泄露构建密钥
构建密钥是只在构建 Docker 镜像时需要的凭证(不是在运行时)。例如,你可能想在你的镜像中包含某个应用程序的一个编译版本,这个应用的源代码是闭源的,并且其 Git 存储库是有访问保护的。在构建镜像时,你需要克隆 Git 存储库(这需要构建密钥,例如该存储库的 SSH 访问密钥),从源代码构建应用程序,然后再删除源代码(和密钥)。
“泄露“构建密钥是说你不小心把这种密钥烘焙到了你的镜像的某个层中。这种情况很严重,因为拉取你的镜像的所有人都可以检索到这些机密。这个问题源于这样一个事实,即 Docker 镜像是以纯粹的加法方式逐层构建的。你在一个层中删除的文件只是被“标记”为已删除,但拉取你镜像的人们仍然可以使用高级工具访问它们。
可以使用以下两种方法之一来避免泄露构建密钥。
多阶段构建
Docker 多阶段构建(官方文档)有许多用例,例如加快你的镜像构建速度,或减少镜像大小。本系列的其他文章会详细介绍其他用例。总之,你也可以通过多阶段构建来避免泄露构建密钥,如下所示:
-
创建一个阶段 #A,将凭证复制到其中,并使用它们来检索其他工件(例如上述例子中的 Git 存储库)和执行进一步的步骤(例如编译一个应用程序)。阶段 #A 的构建确实包含了构建的密钥!
-
创建一个 #B 阶段,其中你只从 #A 阶段复制非加密的工件,例如一个已编译的应用程序。
-
只发布 / 推送阶段 #B 的镜像
BuildKit 的密钥
背景知识
如果你使用 docker build 进行构建,可以实际执行构建的后端选项不止一个。其中较新和较快的后端是 BuildKit,你需要在 Linux 上设置环境变量 DOCKER_BUILDKIT=1 来显式启用它。注意,BuildKit 在 Windows/MacOS 的 Docker for Desktop 上是默认启用的。
正如这里的文档所解释的(阅读它们以了解更多细节),BuildKit 构建引擎支持 Dockerfile 中的额外语法。要使用构建密钥,请在你的 Dockerfile 中放入类似下面这样的内容:
RUN --mount=type=secret,id=mysecret,dst=/foobar <command to run>
当 RUN 语句被执行时,密钥将对这个构建容器可用,但不会将密钥本身(这里是:/foobar 文件夹)放入构建的镜像中。你需要在运行 docker build 命令时指定密钥的源文件 / 文件夹(位于主机上)的路径,例如:
docker build --secret id=mysecret,src=mysecret.txt -t sometag
不过有一点需要注意:你不能通过 docker-compose up --build 来构建需要密钥的镜像,因为 Docker-compose 还不支持用于构建的 --secret 参数,见 GitHub 问题。如果你依赖 docker-compose 的构建,请使用方法 1(多阶段构建)。
题外话:不要推送在开发机上构建的镜像
你应该一直在一个干净的环境中构建和推送镜像(例如 CI/CD 管道),其中构建代理会将你的存储库克隆到一个新目录。
使用本地开发机器进行构建的问题是,你的本地 Git 存储库的“工作树“可能是脏的。例如,它可能包含有开发过程中需要的密钥文件,例如对中转甚至生产服务器的访问密钥。如果没有通过.dockerignore 排除这些文件,那么 Dockerfile 中的“COPY . .“等语句可能会意外导致这些密钥泄露到最终镜像中。
03 以非 root 用户身份运行
默认情况下,当有人通过“docker runyourImage:yourTag“运行你的镜像时,这个容器(以及你在 ENTRYPOINT/CMD 中的程序)会以 root 用户身份运行(在容器和主机上)。这给了一个使用某种漏洞在你的运行容器中获得 shell 权限的攻击者以下权力:
-
对主机上所有显式挂载到容器中的目录的无限制写权限(因为是 root)。
-
能够在容器中做 Linux 根用户可以做的一切事情。例如,攻击者可以安装他们需要的额外工具来加载更多的恶意软件,比如说通过 apt-get install(非 root 用户无法做到这一点)。
-
如果你的镜像容器是用 docker run --privileged 启动的,攻击者甚至可以接管整个主机。
为了避免这种情况,你应该以非 root 用户(你在 docker build 过程中创建的一些用户)的身份运行你的应用程序。在你的 Dockerfile 中的某个地方(通常是在结尾处)放置以下语句:
# Create a new user (including a home-directory, which is optional)
RUN useradd --create-home appuser
# Switch to this user
USER appuser
Dockerfile 中所有在 USER appuser 语句之后的命令(如 RUN、CMD 或 ENTRYPOINT)都将以这个用户运行。这里有一些需要注意的地方:
-
在切换到非 root 用户之前,你通过 COPY 复制到镜像中的文件(或由某些 RUN 命令创建的文件)是由 root 用户拥有的,因此以非 root 用户身份运行的应用程序无法写入。为了解决这个问题,请把创建和切换到非 root 用户的代码移到 Dockerfile 的开头。
-
如果这些文件是在 Dockerfile 的开头以根用户身份创建的(存储在 /root/ 下面,而不是 /home/appuser/ 下面),那么你的程序期望在用户的主目录中的某个地方(例如~/.cache)的文件,现在从应用程序的视角来看可能突然消失了。
-
如果你的应用程序监听一个 TCP/UDP 端口,就必须使用大于 1024 的端口。小于等于 1024 的端口只能以 root 用户身份使用,或者以一些高级 Linux 能力来使用,但你不应该仅仅为了这个目的而给你的容器这些能力。
04 使用最新的基础镜像构建和更新系统包
如果你使用的基础镜像包含了某个真正的 Linux 发行版(如 Debian、Ubuntu 或 alpine 镜像)的全部工具集,其中包括一个软件包管理器,建议使用该软件包管理器来安装所有可用的软件包更新。
背景知识
基础镜像是由某人维护的,他配置了 CI/CD 管道计划来构建基础镜像,并定期推送到 Docker Hub。你无法控制这个时间间隔,而且经常发生的情况是,在该管道将更新的 Docker 镜像推送到 Docker Hub 之前,Linux 发行版的包注册表(例如通过 apt)中已经有了安全补丁。例如,即使基础镜像每周推送一次,也有可能在最近的镜像发布几小时或几天后出现安全更新。
因此,最好总是运行更新本地软件包数据库和安装更新的包管理器命令,采用无人值守模式(不需要用户确认)。每个 Linux 发行版的这个命令都不一样。
例如,对于 Ubuntu、Debian 或衍生的发行版,使用 RUN apt-get update && apt-get -y upgrade
另一个重要的细节是,你需要告诉 Docker(或你使用的任何镜像构建工具)来刷新基础镜像。否则,如果你引用一个基础镜像,比如 python:3(而 Docker 在其本地镜像缓存中已经有了这样一个镜像),Docker 甚至不会检查 Docker Hub 上是否存在更新的 python:3 版本。为了摆脱这种行为,你应该使用这个命令:
docker build --pull <rest of the build command>
这可以确保 Docker 在构建镜像之前拉取你的 Dockerfile 中 FROM 语句中提到的镜像的更新。
你还应该注意 Docker 的层缓存机制,它会让你的镜像变得陈旧,因为 RUN <install apt/etc. updates>命令的层是缓存的,直到基础镜像维护者发布新版本的基础镜像才刷新。如果你发现基础镜像的发布频率相当低(比如少于一周一次),那么定期(比如每周一次)重建你的镜像并禁用层缓存是个好主意。你可以运行以下命令来做到这一点:
docker build --pull --no-cache <rest of the build command>
05 定期更新第三方依赖
你编写的软件是基于第三方的依赖,也就是由其他人制作的软件。这包括了:
-
你的镜像下面的基础 Docker 镜像,或
-
你作为自己应用程序的一部分使用的第三方软件组件,例如通过 pip/npm/gradle/apt/……安装的组件。
如果你的镜像中的这些依赖过时了,就会增加攻击面,因为过时的依赖往往有可利用的安全漏洞。
你可以定期使用 SCA(软件组件分析)工具来解决这个问题,比如 Renovate Bot。这些工具(半)自动将你声明的第三方依赖更新为最新版本,例如在你的 Dockerfile、Python 的 requirements.txt、NPM 的 packages.json 等文件中声明的列表。你需要设计你的 CI 管道,使 SCA 工具所做的更改自动触发你的镜像的 re-build。
这种自动触发的镜像重建对于处在只维护模式,但代码仍将被客户在生产环境中使用(客户希望它是安全的)的项目特别有用。在维护期间,你不再开发新的特性,也不会构建新的镜像,因为没有新的提交(由你做出)来触发新的构建。然而,由 SCA 工具做出的提交确实会再次触发镜像构建。
你可以在我的相关博文中找到更多关于 Renovate bot 的细节。
06 对你的镜像进行漏洞扫描
即使你执行了上述建议,比如说你的镜像总是使用最新的第三方依赖,它仍然可能是不安全的(例如一个依赖已经被弃用的情况)。在这种情况下,“不安全“意味着一个(或多个)依赖有已知的安全漏洞(在一些 CVE 数据库中注册)。
出于这个原因,你可以给你的 Docker 镜像提供某种工具来扫描所有包含的文件,以找到这种漏洞。这些工具有两种形式:
-
你显式调用的 CLI 工具(例如在 CI 管道中),比如说 Trivy(OSS,在 CI 管道中非常容易使用,见 Trivy 文档)、Clair(OSS,但设置和使用比 Trivy 更复杂),或 Snyk(通过“docker scan“集成到 Docker CLI 中,见 cheat sheet,但只有有限的免费计划!)
-
集成到你推送镜像的镜像注册中心的扫描器,如 Harbor(内部使用 Clair 或 Trivy)。还有一些商业产品,如 Anchore。
因为这些扫描器是通用的,它们还试图覆盖一大堆包注册表,所以可能不会特别为你在自己项目中使用的编程语言或包注册表定制。有时,你应该调查你的编程语言生态系统提供了哪些工具。例如,对于 Python 来说就有一个专门针对 Python 包的安全工具。
07 扫描你的 Dockerfile 是否违反了最佳实践
有时,问题来自于你在 Dockerfile 中放置的语句,这些语句是不好的实践(但你没有意识到)。为此可以使用诸如 checkov、Conftest、trivy 或 hadolint 等工具,它们是 Dockerfile 的 linter。为了选择正确的工具,你需要查看它的默认规则 / 政策。例如,hadolint 比 checkov 或 conftest 提供的规则更多,因为它是专门针对 Dockerfiles 的。这些工具也是相互补充的,因此在你的 Dockerfiles 上运行多个工具(如 hadolint 和 trivy)确实是有意义的。不过要做好准备,因为你需要维护“忽略文件“,在这个文件中的规则会被忽略——可能是由于误报而有意忽略它们,或者是你准备故意破坏规则。
08 不要对 Docker Hub 使用 Docker 内容信任
为了验证你使用的基础镜像确实是由该镜像背后的公司构建和推送的,你可以使用 Docker 内容信任(见官方文档)特性。只需在运行 docker build 或 docker pull 时将 DOCKER_CONTENT_TRUST 环境变量设为“1“即可启用该特性。Docker 守护进程将拒绝提取没有经过发布者签名的镜像。
不幸的是,大约一年前开始社区就不再以这种方式签名镜像了。就连 Docker Inc. 也在 2020 年 12 月停止了签名官方 Docker 镜像,也没有官方解释。问题更大的是如果你使用“docker pull docker:latest”这样的命令,只会下载一个过时很久的镜像。
你可以查看一下镜像签名的其他实现,比如说 cosign(不过我还没试过)。
09 扫描你自己的代码是否有安全问题
安全问题通常来源于其他人的代码,也就是流行的第三方依赖。因为它们应用广泛,所以在黑客那里是“有利可图“的。然而,有时是你自己的代码在作怪。例如,你可能不小心实现了 SQL 注入的可能性、堆栈溢出的错误,等等。
为了找到这些问题,你可以使用所谓的 SAST(静态应用安全测试)工具。一方面,有一些特定于编程语言的工具(你必须单独研究),如 Python 的 bandit,或 Java 的 Checkstyle/Spotbugs。另一方面,还有一些支持多种编程语言和框架的工具套件(其中一些是非免费 / 商业的),如 SonarQube(对于它还有 SonarLint IDE 插件)。
在实践中,安全扫描有两种基本方法:
-
连续(自动)扫描:你创建一个 CI 作业,在每次推送时扫描你的代码。这可以让你的代码安全性保持在一个较高的水平上,但你必须弄清楚如何忽略误报(这是一项持续的维护工作)。如果你使用 GitLab,可能还会发现 GitLab 的免费 SAST 功能很有趣。
-
不定期(手动)扫描:团队中一些有安全意识的成员在本地运行安全检查,例如每月一次或每次发布前,并手动查看结果。
10 使用 docker-slim 来删除不必要的文件
docker-slim 工具可以获取大型 Docker 镜像,临时运行它们,分析哪些文件在临时容器中是被真正使用的,然后生成一个新的、单层的 Docker 镜像——其中所有未使用的文件都会被删除。这样做有两个好处:
-
镜像被缩小
-
镜像变得更加安全,因为不需要的工具被删除了(例如 curl 或包管理器)。
请参考我之前文章中的 Docker slim 部分以了解更多细节。
11 使用最小的基础镜像
一个镜像中存储的软件(如 CLI 工具等)越多,攻击面就越大。使用“最小“的镜像是一个很好的实践,它越小越好(无论如何这是一个很好的优势),并且应该包含尽可能少的工具。最小的镜像甚至超越了“优化体积“的镜像(如 alpine 或:-slim,如 python:3.8-slim):前者没有任何包管理器。这使攻击者很难加载额外的工具。
最安全的最小基础镜像是 SCRATCH,它完全不包含任何东西。只有当你在镜像中放置自包含的二进制文件时,才能用 FROM SCRATCH 启动你的 Dockerfile——这些二进制文件烘焙进了所有的依赖(包括 C-runtimes)。
如果 SCRATCH 不适合你,谷歌的无发行版(distroless)镜像可以是一个很好的选择,特别是当你正在为常见的编程语言(如 Python 或 Node.js)构建应用程序,或者需要一个最小的 Debian 基础镜像时。
不幸的是,最小镜像有几个需要注意的地方:
-
无发行版的注意事项:
-
不建议使用谷歌在 gcr.io 上发布的针对特定编程语言的镜像,因为那里只有一个 latest 版本标签,以及 major 版本的标签(例如 python 的“3“,或 Node 的“12“)。你无法控制具体的语言运行时版本(例如是否使用 Python 3.8.3 或 3.8.4 等),这破坏了你的镜像构建的可重用性。
-
定制(和构建你自己的)无发行版镜像是相当复杂的:你需要熟悉 Bazel 的构建系统并自己构建镜像。
-
注意:如果你唯一需要的定制是“以非 root 用户身份运行代码”,那么每个无发行版基础镜像中都有一个默认的非 root 用户,详见这里。
-
-
-
最小基础镜像的常规注意事项:
-
使用最小基础镜像调试容器是很棘手的,因为有用的工具(比如 /bin/sh)现在不见了。
-
对于 Docker,你可以运行第二个调试容器(它确实有一个 shell 和调试工具,例如 alpine:latest),并使其共享你的最小容器的 PID 命名空间,例如通过 docker run -it --rm --pid=container:
--cap-add SYS_PTRACE alpine sh -
对于 Kubernetes,你可以使用短期容器,见这里的例子
-
-
12 使用受信任的基础镜像
一个受信任的镜像指的是经过某人(要么是你自己的组织,要么是其他人)按照比如说某种安全级别审核的镜像。这对具有高安全要求和规定的受管制行业(银行、航空航天等)来说可能特别重要。
虽然你自己可以通过从头开始建立可信的镜像来完成审计工作,但这是不可取的。因为你(这个镜像的构建者)必须确保所有与审计有关的任务都已完成,并有正确的记录(例如记录镜像中的包列表、执行的 CVE 检查及其结果等等)。这项任务非常繁重。相反,我们建议将这项工作外包出去,使用商业性的“可信注册表“——它提供了一套选定的可信镜像,如 RedHat 的通用基础镜像(UBI)。RedHat 的 UBI 现在也可以在 Docker Hub 上免费获取。
背景知识
在 Docker Hub 上托管的镜像没有经过审计。它们是“按原样“提供的。它们可能是不安全的(甚至包含恶意软件),而且没有人会通知你这一点。因此,使用 Docker Hub 中不安全的基础镜像也会让你的镜像变得不安全。
另外,你不应该把审计和上面提到的 Docker 的内容信任混为一谈!内容信任只确认来源(镜像上传者)的身份,并不会确认与镜像安全性有关的任何事实。
13 测试你的镜像是否能在降低能力的情况下工作
Linux capabilities 是 Linux 内核的一个特性,它允许你控制一个应用程序可以使用哪些内核特性,例如一个进程是否可以发送信号(如 SIGKILL)、配置网络接口、挂载磁盘,或调试进程等。完整的列表见这里。一般来说,你的应用程序需要的功能越少越好。
启动你的镜像容器的所有人都可以给予(或拿走)这些能力,例如通过调用“docker run --cap-drop=ALL “。默认情况下 Docker 会放弃所有能力,除了这里定义的那些以外。你的应用程序可能不需要所有这些功能。
作为一个最佳实践,你可以尝试启动你的镜像容器,放弃所有能力(使用 --cap-drop=ALL),看看它是否仍然正常工作。如果不能,请搞清楚哪些功能是缺失的,并且你是否真的需要它们。然后请记录你的镜像需要哪些功能(以及为什么),这会给运行你镜像的用户带去更多信心。
14 总结
提升你的镜像安全性并非闲庭信步。你需要花时间来评估和实施每一种实践。本文中的列表应该可以节省你的时间,因为收集和排序重要步骤的工作已经为你做好了。
所幸,提升你的应用程序的安全性是一个迭代过程。你可以从小处着手,每次实施一个步骤。不过你确实需要得到管理层的支持。这有时是很难办的,特别是有时你的经理会对建议有抵触情绪,他们可能倾向于从过去的经验来做推断(“我们,或我们的客户,以前从未被黑过,那么为什么这种问题现在会发生在我们身上?我们需要的是特性!“)。你有几个选择:可以说服你的经理为安全性分配资源。例如,如果你有一个直接接触客户的渠道(你在为其构建软件),那么可以说服他们,让他们要求把安全性作为一个“特性“。或者在你的行业中寻找安全漏洞的报告(例如一个直接的竞争对手受到了影响),以证明黑客攻击确实发生了,甚至你的行业也出了问题,而且它们有严重的(财务)影响。
40 个优化 PHP 代码的小窍门 (英文)
40 Tips for optimizing your php Code
1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
2. echo is faster than print.
3. Set the maxvalue for your for-loops before and not in the loop.
4. Unset your variables to free memory, especially large arrays.
5. Avoid magic like __get, __set, __autoload
6. require_once() is expensive
7. Use full paths in includes and requires, less time spent on resolving the OS paths.
8. If you need to find out the time when the script started executing, $_SERVER[’REQUEST_TIME’] is preferred to time()
9. See if you can use strncasecmp, strpbrk and stripos instead of regex
10. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
11. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
12. Error suppression with @ is very slow.
13. $row[’id’] is 7 times faster than $row[id]
14. Error messages are expensive
15. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.
16. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
17. Incrementing a global variable is 2 times slow than a local var.
18. Incrementing a object property (eg. $this->prop++) is 3 times slower than a local variable.
19. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
20. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
21. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
22. Methods in derived classes run faster than ones defined in the base class.
23. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.
24. Surrounding your string by '' instead of " will make things interpret a little faster since php looks for variables inside "..." but not inside ''...''. Of course you can only do this when you don''t need to have variables in the string.
25. When echoing strings it''s faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments.
26. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.
27. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times.
28. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load
29. Use ip2long() and long2ip() to store IP addresses as integers instead of strings in a database. This will reduce the storage space by almost a factor of four (15 bytes for char(15) vs. 4 bytes for the integer), make it easier to calculate whether a certain address falls within a range, and speed-up searches and sorts (sometimes by quite a bit).
30. Partially validate email addresses by checking that the domain name exists with checkdnsrr(). This built-in function checks to ensure that a specified domain name resolves to an IP address. A simple user-defined function that builds on checkdnsrr() to partially valid email addresses can be found in the user comments section in the PHP docs. This is handy for catching those occasional folks who think their email address is ‘joeuser@wwwphp.net’ instead of ‘joeuser@php.net’.
31. If you’re using PHP 5 with MySQL 4.1 or above, consider ditching the mysql_* functions for the improved mysqli_* functions. One nice feature is that you can use prepared statements, which may speed up queries if you maintain a database-intensive website. Some benchmarks.
32. Learn to love the ternary operator.
33. If you get the feeling that you might be reinventing the wheel during a project, check PEAR before you write another line. PEAR is a great resource that many PHP developers are aware of, yet many more are not. It’s an online repository containing over 400 reusable snippets that can be dropped right into your PHP application. Unless your project is trully unique, you ought to be able to find a PEAR package that saves at least a little time. (Also see PECL)
34. Automatically print a nicely formatted copy of a page’s source code with highlight_file().This function is handy for when you need to ask for some assistance with a script in a messageboard, IRC, etc. Obviously, some care must be taken not to accidently show your source when it contains DB connection information, passwords, etc.
35. Prevent potentially sensitive error messages from being shown to users with the error_reporting(0) function. Ideally error reporting should be completely disabled on a production server from within php.ini. However if you’re on a shared webhost and you aren’t given your own php.ini, then your best bet is to add error_reporting(0); as the first line in each of your scripts (or use it with require_once().) This will prevent potentially sensitive SQL queries and path names from being displayed if things go awry.
36. Use gzcompress() and gzuncompress() to transparently compress/decompress large strings before storing them in a database. These built-in functions use the gzip algorithm and can compress plaintext up to 90%. I use these functions almost everytime I read/write to a BLOB field within PHP. The only exception is when I need full text indexing capabilities.
37. Return multiple values from a function with “by reference” parameters. Like the ternary operator, most PHP developers who come from a more formalized programming background already know this one. However, those who’s background is more HTML than Pascal, probably have wondered at one time “how do I get multiple values back from a function I wrote, even though I can only use one return value?” The answer is that you precede a variable with “&” and use it “by reference” instead of “by value”.
38. Fully understand “magic quotes” and the dangers of SQL injection. I’m hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it’s absolutely critical to understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.
39. When working with strings and you need to check that the string is either of a certain length you''d understandably would want to use the strlen() function. This function is pretty quick since it''s operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using a isset() trick.
Ex.
if (strlen($foo) < 5) { echo "Foo is too short"; }
vs.
if (!isset($foo{5})) { echo "Foo is too short"; }
Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it''s execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string''s length.
40. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don''t go modifying your C or Java code thinking it''ll suddenly become faster, it won''t. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend''s PHP optimizer. It is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
41. Excellent Article about optimizing php by John Lim
Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)
ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)
原因:系统盘满了
[root@localhost opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 17G 0 100% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 477M 80M 372M 18% /boot
[root@localhost opt]#
解决:
删除大文件后,重启系统解决
[root@localhost mysql]# /opt/lampp/lampp status
Version: XAMPP for Linux 1.8.3-3
Apache is not running.
MySQL is not running.
ProFTPD is running.
df: 未处理文件系统
[root@localhost opt]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
18G 17G 0 100% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/sda1 477M 80M 372M 18% /boot
[root@localhost opt]#
[root@localhost ~]# /opt/lampp/lampp status
Version: XAMPP for Linux 1.8.3-3
Apache is not running.
MySQL is running.
ProFTPD is running.
转
xampp 无法启动mysql 找不到mysql.sock
(2016-02-24 23:21:24)分类: 技术 |
如果xampp中的mysql启动不了,出现ERROR 2002 (HY000): Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)报错,
停止xampp的时候报:
-bash-4.1# /opt/lampp/lampp stop
Stopping XAMPP for Linux 1.8.2-6...
XAMPP: Stopping Apache...ok.
XAMPP: Stopping MySQL...ok.
XAMPP: Stopping ProFTPD...kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
fail.
解决办法:
如果网上一些方法不好用的话,可以试试以下方法:
1. 确定系统盘是否满了
#df -h
2. 删除/opt/lampp目录中的pid文件(删掉后xampp重启时会重建,如果不放心,可以先备份lampp目录)
删除mysql相关缓存:
#rm -rf /opt/lampp/var/mysql/VM_*
删除proftp相关缓存:
#rm -rf /opt/lampp/var/proftpd.pid
如果找不到pid文件,可以搜一下:
#find /opt/lampp -name ''*.pid''
Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''
MySQL已经被我移到数据盘了,本地连接数据库会报错:Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''
但是远程是可以连接的,my.cnf设置mysql的根目录也改成了数据盘的地址,还要在加上client的参数,设置如下:
[client]
socket = /home/data/mysql/mysql.sock
之后重启下mysql就可以了
今天关于30 个优化 MySQL 语句的技巧和如何优化mysql语句的讲解已经结束,谢谢您的阅读,如果想了解更多关于12 个优化 Docker 镜像安全性的技巧、40 个优化 PHP 代码的小窍门 (英文)、Can''t connect to local MySQL server through socket ''/opt/lampp/var/mysql/mysql.sock'' (2)、Can''t connect to local MySQL server through socket ''/var/lib/mysql/mysql.sock''的相关知识,请在本站搜索。
本文标签: