linux 学习笔记-037-LAMP 介绍,MySQL 和 MariaDB 介绍,MySQL 安装

发布于 2018-02-26  397 次阅读


LAMP 架构介绍

包含:Linux+Apache(httpd)+MySQL+PHP

linux 学习笔记-037-LAMP 介绍,MySQL 和 MariaDB 介绍,MySQL 安装

PHP:脚本语言,以做网站为主

Apache/MySQL/PHP:三者可以部署在一台机器上,也可以分开(Apache 和 PHP 要在一起)

httpd、PHP、MySQL 三者如何工作(在 LAMP 中,PHP 是作为 httpd 的一个模块存在)

linux 学习笔记-037-LAMP 介绍,MySQL 和 MariaDB 介绍,MySQL 安装

MySQL_MariaDB 介绍

MySQL 是一个关系型数据库,由 mysql ab 公司开发,mysql 在 2008 年被 sun 公司收购(10 亿刀),2009 年 sun 公司被 oracle 公司收购(74 亿刀)

MySQL 官网 https://www.mysql.com  最新版本 5.7GA/8.0DMR

MySQL5.6 变化比较大,5.7 性能上有很大提升

Mariadb 为 MySQL 的一个分支,官网 https://mariadb.com/  最新版本 10.2

MariaDB 主要由 SkySQL 公司(现更名为 MariaDB 公司)维护,SkySQL 公司由 MySQL 原作者带领大部分原班人马创立.

Mariadb5.5 版本对应 MySQL 的 5.5,10.0 对应 MySQL5.6

Community 社区版本,Enterprise 企业版,GA(Generally Available)指通用版本,在生产环境中用,DMR(Development Milestone Release)开发里程碑发布版,RC(Release Candidate)发行候选版本,Beta 开放测试版本,Alpha 内部测试版本

MySQL 安装

MySQL 的几个常用安装包:rpm 包、源码包、二进制免编译包

rpm 包:方便,但无法指定安装目录

二进制免编译包:方便,可以指定安装目录,但性能不一定能保证,但是如果没其他的特殊要求,建议使用

源码包:操作复杂,容易出错,但可以随心所欲

这里使用二进制免编译包安装:

[root@am-01:~#] cd /usr/local/src

[root@am-01:/usr/local/src#] wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

[root@am-01:/usr/local/src#] tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

[root@am-01:/usr/local/src#] mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql

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

[root@am-01:/usr/local/mysql#] useradd mysql

[root@am-01:/usr/local/mysql#] mkdir /data/

[root@am-01:/usr/local/mysql#] ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

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

Data::Dumper

#以上提示缺少某些包

[root@am-01:/usr/local/mysql#] yum list | grep perl | grep -i dumper

Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

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

perl-Data-Dumper-Concise.noarch           2.020-6.el7                  epel     

perl-Data-Dumper-Names.noarch             0.03-17.el7                  epel     

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

#按关键字搜索包名,也可以在百度、谷歌、bing 搜索错误提示

#这里搜索出来 4 个相关的包,如果不知道哪一个包是,可以一个一个安装尝试

[root@am-01:/usr/local/mysql#] yum -y install perl-Data-Dumper.x86_64

已加载插件:fastestmirror

Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast

base                                                                         | 3.6 kB  00:00:00     

epel/x86_64/metalink                                                         | 6.3 kB  00:00:00     

epel                                                                         | 4.7 kB  00:00:00     

extras                                                                       | 3.4 kB  00:00:00     

updates                                                                      | 3.4 kB  00:00:00     

………………省略部分输出信息………………

Downloading packages:

perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                      |  47 kB  00:00:05     

Running transaction check

Running transaction test

Transaction test succeeded

Running transaction

  正在安装    : perl-Data-Dumper-2.145-3.el7.x86_64                                             1/1

  验证中      : perl-Data-Dumper-2.145-3.el7.x86_64                                             1/1



已安装:

  perl-Data-Dumper.x86_64 0:2.145-3.el7                                                             



完毕!

[root@am-01:/usr/local/mysql#] ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

WARNING: The host 'am-01' could not be looked up with ./bin/resolveip.

This probably means that your libc libraries are not 100 % compatible

with this binary MySQL version. The MySQL daemon, mysqld, should work

normally with the exception that host name resolving will not work.

This means that you should use IP addresses instead of hostnames

when specifying MySQL privileges !

………………省略部分输出信息………………

WARNING: Default config file /etc/my.cnf exists on the system

This file will be read by default by the MySQL server

If you do not want to use this, either remove it, or use the

--defaults-file argument to mysqld_safe when starting the server

[root@am-01:/usr/local/mysql#] echo $?

0

#检查是否安装成功(两个 OK 字眼),或者 echo $?(为 0 即安装正常,必须在刚做完安装操作后做,$?反映的是上一条命令的正确性)

[root@am-01:/usr/local/mysql#] rpm -qf /etc/my.cnf

mariadb-libs-5.5.41-2.el7_0.x86_64

#这里可以看到,CentOS7 默认会安装 mariadb,所以这里就需要把 mariadb 的 my.cnf 替换为 mysql 的 my.cnf,也可以修改这个配置文件的设置,让其支持 mysql

[root@am-01:/usr/local/mysql#] vim /etc/my.cnf



[mysqld]

datadir=/data/mysql

socket=/tmp/mysql.sock

# 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

#

#!includedir /etc/my.cnf.d

#这里是修改原有的/etc/my.cnf 配置文件

[root@am-01:/usr/local/mysql#] cp support-files/my-default.cnf  /etc/my.cnf

#这里是拷贝模板配置文件(mysql 的配置文件默认要叫 my.cnf,并且默认要在/etc 目录下)

[root@am-01:/usr/local/mysql#] cp support-files/mysql.server /etc/init.d/mysqld

[root@am-01:/usr/local/mysql#] vim /etc/init.d/mysqld

basedir=/usr/local/mysql

datadir=/data/mysql

#定义 basedir 和 datadir

[root@am-01:/usr/local/mysql#] chkconfig --add mysqld

[root@am-01:/usr/local/mysql#] 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:关

[root@am-01:/usr/local/mysql#] service mysqld start

Starting MySQL.Logging to '/data/mysql/am-01.err'.

... SUCCESS!

#拷贝 mysql 的启动模板到/etc/init.d/目录下,并把 mysql 添加到系统启动项并启动

[root@am-01:/usr/local/mysql#] ps -aux | grep mysql

root      55926  0.0  0.1  11776  1576 pts/0    S    00:19   0:00 /bin/sh /usr/local/mysql/bin/mysqd_safe --datadir=/data/mysql --pid-file=/data/mysql/am-01.pid

mysql     56063  6.0 44.8 1300776 449540 pts/0  Sl   00:19   0:03 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/am-01.err --pid-file=/data/mysql/am-01.pid --socket=/tmp/mysql.sock

root      56100  0.0  0.0 112672   976 pts/0    S+   00:20   0:00 grep --color=auto mysql

#可以见到 mysql 已经启动

[root@am-01:/usr/local/mysql#] service mysqld stop

Shutting down MySQL.. SUCCESS!

#停止 mysql

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

[1] 56220

[root@am-01:/usr/local/mysql#] 180227 00:26:06 mysqld_safe Logging to '/data/mysql/am-01.err'.

180227 00:26:06 mysqld_safe Starting mysqld daemon with databases from /data/mysql



[root@am-01:/usr/local/mysql#] ps -aux | grep mysql

root      56220  0.5  0.1 113268  1600 pts/0    S    00:26   0:00 /bin/sh /usr/local/mysql/bin/mysqd_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql

mysql     56345 29.0 44.8 1300780 449524 pts/0  Sl   00:26   0:01 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysq/lib/plugin --user=mysql --log-error=/data/mysql/am-01.err --pid-file=/data/mysql/am-01.pid --socket=/tmp/mysql.sock

root      56368  0.0  0.0 112672   976 pts/0    S+   00:26   0:00 grep --color=auto mysql

#以上是使用命令行的方式启动 mysql(适用于没有启动模板的情况下)

[root@am-01:/usr/local/mysql#] killall mysqld

[root@am-01:/usr/local/mysql#] 180227 00:30:26 mysqld_safe mysqld from pid file /data/mysql/am-01.pid ended



[1]+  完成                  /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql

[root@am-01:/usr/local/mysql#] ps -aux | grep mysql

root      56535  0.0  0.0 112672   976 pts/0    S+   00:30   0:00 grep --color=auto mysql

#把用命令行的方式启动的 mysql 停止

#killall 会先等目前的操作先停止才会把进程杀掉(数据一般不会丢失),kill 会直接把进程杀掉(可能导致数据丢失)

扩展

mysql5.5 源码编译安装:

http://www.aminglinux.com/bbs/thread-1059-1-1.html

mysql5.7 二进制包安装(变化较大):

http://www.apelearn.com/bbs/thread-10105-1-1.html