7.27 11.1-11.5

11.1 LAMP架构介绍

LAMPLinux+Apache(即httpd+MySQL+PHP

Apache:提供web服务的软件

MySQL:用于存储字符串的数据库软件,无法存储图片、声音等文件,主要用于存储文字性内容(帖子、消息、用户名密码信息、账户积分信息等)

PHP:

一个脚本语言,类似shell,但比shell复杂,使用C语言开发,主要用于做网站,用PHP写的网站多用于PC端访问;

移动app也需要调用服务端的接口,在服务端取数据,假如数据存在mysqlmysql会和PHP通信,所以服务端的程序也可能是PHP写的

安装时Apache必须和PHP在一台服务器,其他服务可以分别装在不同的服务器上

 

LAMP架构如何工作:

blob.png

PHP以一个模块的形式和Apache结合在一起;

Apache无法直接与MySQL通信,只能通过PHP模块去MySQL取数据,然后PHP将数据交给ApacheApache再交给用户;

PHPMySQL相连取数据的行为叫动态请求;

动态请求:

用户登录论坛:

用户在浏览器输入网址并输入用户名密码点击请求登录,此时apache检查用户的请求是动态或静态,若为动态请求,则apache通过PHP模块和MySQL通信查询该用户的密码,然后PHP进行比对,正确则apache会返回登录的状态

静态请求:

网站的logo属于静态文件,此时服务器会从某个目录下或其他服务器拿到logo图片再返回给用户,此时没有用到PHP,不与MySQL通信,属于静态请求

11.2 MySQL_MariaDB介绍

MySQL是一个关系型数据库,由mysql ab公司开发;

2008mysqlsun公司收购;

2009sunOracle公司收购;

Oracle是一家专门做数据库的公司;

小型机一般使用收费的unix操作系统;

mysql最新版本5.7GA/8.0DMR

MySQLOracle收购实现商业化后,原MySQL作者独立创建SkySQL公司(后改名MariaDB公司),该公司开发了MariaDB数据库;

5.5版本的MySQL对应5.5版本的MariaDB5.5版本后MariaDB的版本从10.0开始(对应MySQL5.6版本);

版本区分:

Community:社区版本,可以免费、自由使用

Enterprise:企业版,会提供一些服务,有一些额外的高级功能

GAgenerally available):通用版本,在生产环境中使用,经过反复验证的较稳定的版本

DMR:开发里程碑发布版,通常指产生较大变化的版本

RC:发行候选版本,已经较稳定,作为发行候选版本随时可能被发布

Beta:开放测试版本,该版本还未正式发布,会被开放给用户或公司外的人进行使用测试,反馈问题

Alpha:内部测试版本,软件开发完成后开放前先在公司内部测试、使用

11.3 MySQL安装(上)

MySQL常用安装包:

rpm包、源码包、二进制免编译包

rpm包:直接yum安装

源码包:需要生成可编译文件并编译再安装,所有包都默认装在/usr目录下,无法指定安装路径

二进制免编译:

发布前先在linux服务器进行编译再将编译完成的文件放到一个目录下,再将目录打包压缩并发布;

这种包不需要编译和配置,直接使用即可;

二进制免编译包可以放在任意目录下(即可以指定路径安装);

如果追求服务在机器上运行的最佳性能,一般使用源码包安装;

二进制的包会区分平台(3264位):

确定平台:

[root@hyc-01-01 ~]# uname -a

Linux hyc-01-01 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

一般32位会显示i686i58664位为x86_64

下载mysql二进制免编译包:

[root@hyc-01-01 ~]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

11.4 MySQL安装(中)

解压包:

[root@hyc-01-01 src]# tar zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz

将解压包生成的目录移动位置并改名:

[root@hyc-01-01 src]# mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql

查看移动后的目录:

[root@hyc-01-01 src]# cd /usr/local/mysql

[root@hyc-01-01 mysql]# ls

bin      data  include  man         README   share      support-files

COPYING  docs  lib      mysql-test  scripts  sql-bench

注意:

需要注意/usr/local路径下原先是否存在目录mysql

若存在则mv命令执行的结果可能与正常操作不同导致实验失败;

需要查看/usr/local/mysql目录的内容以确定mv执行结果正确;

创建mysql用户:

[root@hyc-01-01 mysql]# useradd mysql

创建/data/目录:

[root@hyc-01-01 mysql]# mkdir /data/

若该目录已存在则该步骤省略;

约定将下载的包放在/usr/local/src目录下

 

初始化生成/data/mysql目录:

[root@hyc-01-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

--user=mysql 指定用户为mysql

--datadir=/data/mysql 指定存放数据的目录

报错1

FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:

Data::Dumper

报错提示缺少perl模块

在不清楚具体缺少什么模块时进行模糊查找:

[root@hyc-01-01 mysql]# yum list |grep perl|grep -i dump

perl-Data-Dumper.x86_64                     2.145-3.el7                base    

perl-Devel-Symdump.noarch                   1:2.10-2.el7               base    

perl-XML-Dumper.noarch                      0.81-17.el7                base

以上3个包均有可能是报错信息中提到的包,可以尝试安装这些包解决问题;

或者使用浏览器在网络上输入报错信息搜索(百度、binggoogle);

r.aminglinux.com提供谷歌镜像,推荐使用google搜索;

[root@hyc-01-01 mysql]# yum install -y perl-XML-Dumper.noarch

安装以上包后执行初始化,原来的报错依然存在

[root@hyc-01-01 mysql]# yum install -y perl-Data-Dumper.x86_64

[root@hyc-01-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

报错2

Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

原因:缺少libaio库文件

解决:

[root@hyc-01-01 mysql]# yum install -y libaio

[root@hyc-01-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

检查初始化是否成功:

1 查看执行过程中是否有两个ok

2

[root@hyc-01-01 mysql]# echo $?

0 0说明执行成功

拷贝并移动配置文件:

 [root@hyc-01-01 support-files]# ls my-default.cnf mysql模板配置文件路径

my-default.cnf

[root@hyc-01-01 support-files]# pwd

/usr/local/mysql/support-files

[root@hyc-01-01 support-files]# cp my-default.cnf /etc/my.cnf

cp:是否覆盖"/etc/my.cnf" /etc目录下本来就有my.cnf文件

可能系统原先就被安装了mysqlmariaDB所以本来就有my.cnf文件;

mysql的默认配置文件和路径为/etc/my.cnf,想要指定配置文件和路径需要在启动脚本中指定;

 

修改配置文件:

[root@hyc-01-01 support-files]# vim /etc/my.cnf

[mysqld]

datadir=/data/mysql 指定数据存放位置

socket=/tmp/mysql.sock 指定socket通信地址

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

# Settings user and group are ignored when systemd is used.

# If you need to run mysqld under a different user or group,

# customize your systemd unit file for mariadb according to the

# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]

#log-error=/var/log/mariadb/mariadb.log

#pid-file=/var/run/mariadb/mariadb.pid

#

# include all files from the config directory

注释掉的部分不会生效,暂时不看,后期遇到需求再增加即可;

11.5 MySQL安装(下)

mysql启动脚本

[root@hyc-01-01 mysql]# ls support-files/mysql.server

support-files/mysql.server

[root@hyc-01-01 mysql]# pwd

/usr/local/mysql

拷贝启动脚本

[root@hyc-01-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

编辑启动脚本

[root@hyc-01-01 mysql]# vim /etc/init.d/mysqld

#!/bin/sh

# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB

# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based

# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.

# When this is done the mysql server will be started when the machine is

# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux

# chkconfig: 2345 64 36

# description: A very fast and reliable SQL database engine.

# Comments to support LSB init script conventions

### BEGIN INIT INFO

# Provides: mysql

# Required-Start: $local_fs $network $remote_fs

# Should-Start: ypbind nscd ldap ntpd xntpd

# Required-Stop: $local_fs $network $remote_fs

# Default-Start:  2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: start and stop MySQL

# Description: MySQL is a very fast and reliable SQL database engine.

### END INIT INFO

# If you install MySQL on some other places than /usr/local/mysql, then you

# have to do one of the following things for this script to work:

#

# - Run this script from within the MySQL installation directory

# - Create a /etc/my.cnf file with the following information:

#   [mysqld]

#   basedir=<path-to-mysql-installation-directory>

# - Add the above to any other configuration file (for example ~/.my.ini)

#   and copy my_print_defaults to /usr/bin

# - Add the path to the mysql-installation-directory to the basedir variable

#   below.

#

# If you want to affect other MySQL variables, you should make your changes

# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get

# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql 指定mysql程序位置

datadir=/data/mysql 指定mysql数据存放位置

修改启动脚本权限

[root@hyc-01-01 init.d]# ls -l mysqld

-rwxr-xr-x. 1 root root 10592 7  30 07:21 mysqld

确保启动脚本权限为755,默认即755

 

mysqld加入系统服务列表

[root@hyc-01-01 init.d]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

      要列出 systemd 服务,请执行 'systemctl list-unit-files'

      查看在具体 target 启用的服务请执行

      'systemctl list-dependencies [target]'

mysqld           0: 1: 2: 3: 4: 5: 6:

netconsole         0: 1: 2: 3: 4: 5: 6:

network          0: 1: 2: 3: 4: 5: 6:

启动mysql

[root@hyc-01-01 init.d]# /etc/init.d/mysqld start

[root@hyc-01-01 init.d]# service mysqld start

Starting MySQL. SUCCESS! 启动成功

命令行启动mysql(无启动脚本时也可使用):

[root@hyc-01-01 init.d]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &

[1] 28413

[root@hyc-01-01 init.d]# 180730 08:00:12 mysqld_safe Logging to '/data/mysql/hyc-01-01.err'.

180730 08:00:12 mysqld_safe Starting mysqld daemon with databases from /data/mysql

[root@hyc-01-01 init.d]# !ps

ps aux|grep mysql

root     28413  0.2  0.1 113260  1588 pts/0    S    08:00   0:00 /bin/sh /usr/localmysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysq

mysql    28540  6.8 44.6 1302676 449736 pts/0  Sl   08:00   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hyc-01-01.err --pid-file=hyc-01-01.pid --socket=/tmp/mysql.sock

root     28563  0.0  0.0 112676   980 pts/0    R+   08:00   0:00 grep --color=auto mysql

 

查看进程和监听的端口

[root@hyc-01-01 init.d]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name   

tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      849/sshd           

tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      953/master         

tcp        0      0 192.168.31.129:1122     0.0.0.0:*               LISTEN      10724/rsync        

tcp6       0      0 :::22                   :::*                    LISTEN      849/sshd           

tcp6       0      0 ::1:25                  :::*                    LISTEN      953/master         

tcp6       0      0 :::3306                 :::*                    LISTEN      28248/mysqld       

[root@hyc-01-01 init.d]# ps aux|grep mysql

root     28109  0.0  0.1  11772  1572 pts/0    S    07:49   0:00 /bin/sh /usr/localmysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hyc-01-01.pid

mysql    28248  0.4 44.6 1302676 449728 pts/0  Sl   07:49   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hyc-01-01.err --pid-file=/data/mysql/hyc-01-01.pid --socket=/tmp/mysql.sock

root     28273  0.0  0.0 112676   980 pts/0    R+   07:50   0:00 grep --color=auto mysql

关闭mysqld服务

[root@hyc-01-01 init.d]# service mysqld stop

killall杀死mysqld进程:

[root@hyc-01-01 init.d]# yum install -y psmisc

[root@hyc-01-01 init.d]# killall mysqld

[root@hyc-01-01 init.d]# ps aux|grep mysql

root     28772  0.0  0.0 112676   976 pts/0    R+   08:11   0:00 grep --color=auto mysql


猜你喜欢

转载自blog.51cto.com/12216458/2151964