LAMP和LNMP是目前流行的web服务器架构,市场占有率超过80%,所谓LAMP架构,实际上指的是Linux+Apache+MySQL+PHP组合。
LAMP(Linux- Apache-MySQL-PHP)网站架构是目前国际流行的Web框架,所有组成产品均是开源软件,是国际上成熟的架构框架,很多流行的商业应用都是采取这个架构,和 Java/J2EE架构相比,LAMP具有Web资源丰富、轻量、快速开发等特点,微软的.NET架构相比,LAMP具有通用、跨平台、高性能、低价格的 优势,因此LAMP无论是性能、质量还是价格都是企业搭建网站的首选平台。对于大流量、大并发量的网站系统架构来说,除了硬件上使用高 性能的服务器、负载均衡、CDN等之外,在软件架构上需要重点关注下面几个环节:使用高性能的操作系统(OS)、高性能的网页服务器(Web Server)、高性能的数据库(Database)、高效率的编程语言等。
用户通过Apache来访问网站,静态数据由apache直接从系统目录提取,(如网站log,文章,图片,视频之类的数据)动态数据由apache通过php模块从MySQL数据库存取。(如用户名,用户密码,用户输入内容之类的数据)
MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司收购(10亿美金),2009年sun公司被oracle公司收购(74亿美金)
Community社区版本,Enterprise企业版,GA(Generally Available)通用版本,在生产环境中用的,DMR(Development Milestone Release)开发里程碑发布版,RC(Release Candidate)发行候选版本,Beta开放测试版本,Alpha内部测试版本。
直接用yum install -y mysql 来安装客户端,用yum install -y mysql-server ; yum install -y mysql-devel 来安装服务器端。缺点:无法自定义安装路径,默认安装在/usr下。
源码包安装:
下载源码包,并验证
[root@aliyun /]# cd /usr/local/src
[root@aliyun src]# wget
https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.40.tar.gz
下载未编译的源码包
[root@aliyun src]# md5sum mysql-5.6.40.tar.gz
验证md5值是否和官方一致
ba337c0f41f11ed6875665e968c9e20f
mysql-5.6.40.tar.gz
配置安装环境(安装依赖包,
注意mysql源码包配置用的是cmake
)
检查是否已经安装过mysql:
[root@aliyun src]# rpm -qa | grep mysql
若有先卸载:
[root@aliyun src]#rpm -e --nodeps xxx(xxx搜索结果)
删除所有的相关文件:
[root@aliyun src]#rm /etc/my.cnf
安装依赖包:
[root@aliyun src]# yum install -y gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* libtool-ltdl-devel* make bison-devel libaio libaio-devel
cmake
编译安装
1)创建用户
[root@aliyun src]# groupadd mysql
[root@aliyun src]# useradd -r -g mysql -s /sbin/nologin mysql
2)解压,编译安装
[root@aliyun src]# tar -zxvf mysql-5.6.40.tar.gz 解压
源码包
[root@aliyun src]# cd mysql-5.6.40
[root@aliyun mysql-5.6.40]# cmake \
> -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
> -DMYSQL_DATADIR=/usr/local/mysql/data \
> -DSYSCONFDIR=/etc \
> -DWITH_MYISAM_STORAGE_ENGINE=1 \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_MEMORY_STORAGE_ENGINE=1 \
> -DWITH_READLINE=1 \
> -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
> -DMYSQL_TCP_PORT=3306 \
> -DENABLED_LOCAL_INFILE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DEXTRA_CHARSETS=all \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci
配置mysql,然后执行
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
sql/CMakeLists.txt:374 (INCLUDE)
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
libmysqld/CMakeLists.txt:99 (INCLUDE)
-- Library mysqlserver depends on OSLIBS -lpthread;m;crypt;dl;aio
-- Skipping deb packaging on unsupported platform Core.
-- CMAKE_BUILD_TYPE: RelWithDebInfo
-- COMPILE_DEFINITIONS: HAVE_CONFIG_H
-- CMAKE_C_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Wwrite-strings -Wdeclaration-after-statement
-- CMAKE_CXX_FLAGS: -Wall -Wextra -Wformat-security -Wvla -Woverloaded-virtual -Wno-unused-parameter
-- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
WITH_MEMORY_STORAGE_ENGINE
WITH_READLINE
-- Build files have been written to: /usr/local/src/mysql-5.6.40
[root@aliyun mysql-5.6.40]#
看到这个提示说明配置已经完成
官网的配置指导
https://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html
[root@aliyun mysql-5.6.40]# echo $?
0
验证配置是否正确
[root@aliyun mysql-5.6.40]#make
[root@aliyun mysql-5.6.40]# echo $?
0
验证编译是否正确
[root@aliyun mysql-5.6.40]# make install
[root@aliyun mysql-5.6.40]# echo $?
0
验证安装是否正确
3)更改mysql目录属主属组
[root@aliyun ~]# ll -d /usr/local/mysql
drwxr-xr-x 13 root root 4096 May 24 22:30 /usr/local/mysql
[root@aliyun ~]# chown -R mysql.mysql /usr/local/mysql
[root@aliyun ~]# ll -d /usr/local/mysql
drwxr-xr-x 13 mysql mysql 4096 May 24 22:30 /usr/local/mysql
4)初始化数据库
[root@aliyun ~]# cd /usr/local/mysql/scripts
[root@aliyun scripts]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@aliyun scripts]# echo $?
0
验证初始化是否正确
[root@aliyun scripts]#
5)将mysql注册为服务
[root@aliyun scripts]# cd ..
[root@aliyun mysql]# cd support-files/
[root@aliyun support-files]# cp mysql.server /etc/rc.d/init.d/mysql
basedir= /usr/local/mysql
datadir= /usr/local/mysql/data
将服务配置文件拷贝至系统服务目录下,并修改上面2处
chmod 755 /etc/init.d/mysqld 修改权限
[root@aliyun support-files]# cp my-default.cnf /etc/my.cnf
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
server_id = 128
socket = /var/lib/mysql/mysql.sock
将软件配置文件至/etc下,并修改上面5个处
[root@aliyun support-files]# chmod 755 /etc/my.cnf 修改权限
[root@aliyun support-files]# chkconfig --add mysql
将mysql注册为系统服务
[root@aliyun support-files]# chkconfig mysql on
设置mysql开机启动
6)启动MySQL服务
[root@aliyun /]# service mysql start
/etc/init.d/mysql: line 46: /usr/local/mysql: Is a directory
/etc/init.d/mysql: line 47: /usr/local/mysql/data: Is a directory
Starting MySQL.Logging to '/usr/local/mysql/data/aliyun.err'.
180525 00:41:40 mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
The server quit without updating PID file (/usr/local/mysql[FAILED]iyun.pid).
报错!socket文件不存在!解决办法:
将/etc/my.cnf里的
socket = /var/lib/mysql/mysql.sock
改为
socket = /tmp/mysql.sock 即可解决
[root@aliyun /]# service mysql start
/etc/init.d/mysql: line 46: /usr/local/mysql: Is a directory
/etc/init.d/mysql: line 47: /usr/local/mysql/data: Is a directory
Starting MySQL. [
OK
]
启动成功!!
MariaDB安装
二进制免编译安装:
直接在官网下载已经编译过的二进制文件包,解压到要安装路径。优点:免编译且可指定路径,缺点:运行性能可能不是最佳。
步骤:
cd /usr/local/src
进入源码目录
wget http://mirrors.neusoft.edu.cn/mariadb//mariadb-10.2.15/bintar-linux-glibc_214-x86_64/mariadb-10.2.15-linux-glibc_214-x86_64.tar.gz
tar zxvf mariadb-10.2.15-linux-glibc_214-x86_64.tar.gz
下载并解压编译过的二进制文件包
mv mariadb-10.2.15-linux-glibc_214-x86_64
/usr/local/mariadb
将目录移动到安装路径并改名
cd /usr/local/mariadb
进入安装目录
groupadd mariadb
创建mariadb用户组
useradd -r -g mariadb -s /sbin/nologin mariadb
创建mariadb用户
mkdir /usr/local/mariadb/data
创建数据存放目录用于存放mariadb保存的数据
./scripts/mysql_install_db --user=mariadb --basedir=/usr/local/mariadb --datadir=/usr/local/mariadb/data
初始化并指定用户、安装目录和数据文件存放目录
Installing MariaDB/MySQL system tables in '/usr/local/mariadb/data' ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/local/mariadb/bin/mysqladmin' -u root password 'new-password'
'/usr/local/mariadb/bin/mysqladmin' -u root -h aliyun password 'new-password'
Alternatively you can run:
'/usr/local/mariadb/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr/local/mariadb' ; /usr/local/mariadb/bin/mysqld_safe --datadir='/usr/local/mariadb/data'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mariadb/mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
注意上面的输出,排错的时候会用到
[root@aliyun mariadb]# echo $?
因为还有一个mysql数据库在系统里,所以验证一下
0
[root@aliyun mariadb]# ls
bin COPYING.thirdparty data EXCEPTIONS-CLIENT INSTALL-BINARY man README.md scripts sql-bench
COPYING CREDITS docs include lib mysql-test README-wsrep share support-files
[root@aliyun mariadb]# cd support-files/
[root@aliyun support-files]# ls
binary-configure my-huge.cnf my-large.cnf my-small.cnf mysql-log-rotate policy wsrep_notify
magic my-innodb-heavy-4G.cnf my-medium.cnf mysqld_multi.server mysql.server wsrep.cnf
[root@aliyun support-files]#
mariadb配置文件模板有很多,可以根据硬件配置来自定义,此处用
my-small.cnf 和mysql.server
cp my-small.cnf /etc/maria.cnf
复制数据库配置文件模板至/etc下并改为默认的文件名
编辑vim /etc/maria.cnf
修改[client]模块 和 [mysqld]模块下的
port = 3307
mysql已经占用了3306端口
socket = /tmp/mariadb.sock
避免和mysql.sock冲突
没有安装客户client的情况下,可以暂时不管client模块部分
cp mysql.server /etc/init.d/mariadb
复制启动脚本至/etc/init.d下并更名
编辑vim /etc/init.d/mysqld
编辑启动脚本文件
定义 basedir=/usr/local/mariadb
指定mariadb程序安装目录
datadir=/usr/local/mariadb/data
指定数据文件存放目录
conf=/etc/maria.cnf
指定配置文件路径
找到下面的内容,在中间插入
--defaults-file="$conf" 这一段
用来指定配置文件变量
case "$mode" in
'start')
# Start daemon
# Safeguard (relative paths, core dumps..)
cd $basedir
echo $echo_n "Starting MariaDB"
if test -x $bindir/mysqld_safe
then
# Give extra arguments to mysqld with the my.cnf file. This script
# may be overwritten at next upgrade.
$bindir/mysqld_safe
--defaults-file="$conf"
--datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &
wait_for_ready; return_value=$?
chmod 755 /etc/init.d/mariadb
修改权限
chmod 755 /etc/maria.cnf
修改权限
chkconfig --add mariadb
添加至系统服务列表里
/etc/init.d/mariadb start
service mariadb start
systemctl mariadb start
若没有my.cnf这样的启动模板,可用下面的命令指定启动
/usr/local/mariadb/bin/
mysqld_safe
--defaults-file=/etc/maria.cnf --user=mariadb --datadir=/usr/local/mariadb/data
四种方法启动mysqld服务
......SUCCESS 启动成功
结束服务:
/etc/init.d/mariadb stop
service mariadb stop
systemctl mariadb stop
killall ps-name
指定配置文件的启动方式,要用killall+ps-name 的方式来结束服务,不能用kill或kill -9的方式结束进程,因为mysql服务运行时会先在内存读写数据,然后再和磁盘同步,如果kill 直接结束掉进程,有可能会导致数据丢失或损坏列表文件。killall命令会等待服务的所有读写操作结束后才杀死进程,因此不会导致数据丢失或损坏数据列表。