11.1 LAMP架构介绍
11.2 MySQL、MariaDB介绍
11.3/11.4/11.5 MySQL安装
11.1 LAMP架构介绍
~1. Linux+Apache(httpd)+MySQL+PKP 的简称为LAMP架构
Linux指的是操作系统,例如centos
Apache这里的apache指的是提供web的服务软件,他真正的名字是httpd。习惯上叫做Apache
MySQL是存储的一个软件。存的是数据,是一些字符串
PHP是脚本语言。和shell类似但是比shell复杂。像C语言
~2. php网站 (Google、淘宝、百度、51cto博客、猿课论坛)这些都是用PHP写的
~3. Apache、MySQL、PHP这三个角色可以在一台机器、也可以分开(httpd和PHP要在一起,MySQL可以单独一台机器)
~4.httpd、PHP、MySQL三者如何工作
Apache和PHP是一个整体(因为要在一台服务器上),是因为PHP是以一个模块的形式和Apache结合在一起的。Apache不能直接和MySQL相互的打交道,只能通过PHP这个模块去MySQL里面拿数据,拿到数据之后PHP再把这个结果交给Apache,Apache再交给用户。其中,PHP和MySQL相连,去取数据这种的行为叫做 动态的请求
那什么叫动态,是叫静态:比如我们访问猿课论坛,首先我们得登录吧,登录的过程就是动态的过程。就像我们上面说的,我们登录论坛,把账号密码交给Apache,Apache通过PHP去MySQL里面找这个账号密码是不是匹配的。然后再把Apache再把这个结果交给我们用户。这就是动态的请求
我们登录论坛的时候,会有图片,比如论坛的logo。我们用户向Apache请求的时候,他并没有去MySQL打交道。而是去我们linux的某一个目录下去找。这就是静态的请求
MySQL里面不能存图片、文件。那他里面存的是,像用户名 密码、还有我们发的帖子、还有积分
----------------------------------------------------------------------------------------------------------------------------------------------------
11.2 MySQL/MariaDB介绍:
MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司(java)收购(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)发行候选版本(即即将成为GA版本)。Beta开放测试版本(公测)。Alpha内部测试版本(内测)
----------------------------------------------------------------------------------------------------------------------------------------------------
11.3/11.4/11.5 MySQL安装:
MySQL的几个常用的安装包: rpm(直接yum)、源码包、二进制免编译(可指定目录,类似于rpm)
二进制免编译方法:
cd /usr/local/src
wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
tar zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql
cd /usr/local/mysql
useradd mysql
mkdir /data/
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
vi /etc/init.d/mysqld
定义basedir和datadir
basedir=/usr/local/mysql
datadir=/data/mysql
/etc/init.d/mysqld start 用这个命令把mysqld启动起来
(chkconfig --add mysqld 开机启动。chkconfig --list并查看一下列表)
也可以用service mysqld start用这个命令把mysql启动起来
也可以用命令行的方式把它启动起来:
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &
其中--defaults-file=/etc/my.cnf是指定他配置文件所在的路径
用这种命令行的方式把它启动起来,关闭的话,只能用killall mysql,把它kill掉。当然我们也可以用kill pid来杀死这个进程。但是尽量用killall,因为mysql在读写的时候,有一部分还未写入到磁盘,只是暂时保存在内存中,我们要是直接kill pid这个进程的话,容易造成数据丢失。killall的话会等完成读写的时候,在杀死这个进程
所以,注意:当我们在遇到当mysql的进程始终杀不死,等了一分钟还没有杀死,ps aux还会有进程,那说明数据量很大,正在写入到磁盘里去。这个时候不要轻易的用kill -9去杀进程,这样非常有可能丢数据,甚至损坏表
mysql有两个常用的引擎,innodb(比较大) myisam(比较小,存储空间都比较小) 本课只做了解
实例:
[root@localhost ~]# uname -a
root@localhost 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
[root@localhost ~]# cd /usr/local/src
[root@localhost src]#wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# tar -zxvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost src]# cd /usr/local/mysql
[root@localhost mysql]# ls
bin COPYING data docs include lib man mysql-test README scripts share sql-bench support-files
[root@localhost mysql]# useradd mysql
[root@localhost mysql]# mkdir /data/
[root@localhost 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 这个语言是perl脚本的,提示少了perl模块。所以我们要安装。不知道名字,模糊搜索一下
也可去网站将这个提示搜索一下,www.google.com www.bing.com www.baidu.com
[root@localhost mysql]# yum list | grep -i dumper 我们先搜索一下,-i不分大小写。我们如果不清楚到底是依赖哪个包,就全安装上
perl-Data-Dumper.x86_64 2.145-3.el7 @base
perl-XML-Dumper.noarch 0.81-17.el7 base
[root@localhost mysql]# yum install -y perl-Data-Dumper 我们知道,所以就安装这个了
阿鑫在执行那个命令的时候,还提示了Installing MySQL system tables...2018-07-27 23:01:12 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).百度后 安装了libaio*,就解决了(万能的百度)
[root@localhost mysql]# yum install libaio* -y
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.huaweicloud.com
* updates: mirrors.huaweicloud.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 libaio.x86_64.0.0.3.109-13.el7 将被 安装
---> 软件包 libaio-devel.x86_64.0.0.3.109-13.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
===============================================================================================================================================================================================
Package 架构 版本 源 大小
===============================================================================================================================================================================================
正在安装:
libaio x86_64 0.3.109-13.el7 base 24 k
libaio-devel x86_64 0.3.109-13.el7 base 13 k
事务概要
===============================================================================================================================================================================================
安装 2 软件包
总下载量:37 k
安装大小:46 k
Downloading packages:
(1/2): libaio-0.3.109-13.el7.x86_64.rpm | 24 kB 00:00:00
(2/2): libaio-devel-0.3.109-13.el7.x86_64.rpm | 13 kB 00:00:00
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计 107 kB/s | 37 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装 : libaio-0.3.109-13.el7.x86_64 1/2
正在安装 : libaio-devel-0.3.109-13.el7.x86_64 2/2
验证中 : libaio-0.3.109-13.el7.x86_64 1/2
验证中 : libaio-devel-0.3.109-13.el7.x86_64 2/2
已安装:
libaio.x86_64 0:0.3.109-13.el7 libaio-devel.x86_64 0:0.3.109-13.el7
[root@axinlinux-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2019-07-11 11:31:45 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-11 11:31:45 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-11 11:31:45 0 [Note] ./bin/mysqld (mysqld 5.6.43) starting as process 2012 ...
2019-07-11 11:31:45 2012 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-11 11:31:45 2012 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-11 11:31:45 2012 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-11 11:31:45 2012 [Note] InnoDB: Memory barrier is not used
2019-07-11 11:31:45 2012 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-11 11:31:45 2012 [Note] InnoDB: Using Linux native AIO
2019-07-11 11:31:45 2012 [Note] InnoDB: Using CPU crc32 instructions
2019-07-11 11:31:45 2012 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-11 11:31:45 2012 [Note] InnoDB: Completed initialization of buffer pool
2019-07-11 11:31:45 2012 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2019-07-11 11:31:45 2012 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2019-07-11 11:31:45 2012 [Note] InnoDB: Database physically writes the file full: wait...
2019-07-11 11:31:45 2012 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2019-07-11 11:31:46 2012 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2019-07-11 11:31:47 2012 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-07-11 11:31:47 2012 [Warning] InnoDB: New log files created, LSN=45781
2019-07-11 11:31:47 2012 [Note] InnoDB: Doublewrite buffer not found: creating new
2019-07-11 11:31:47 2012 [Note] InnoDB: Doublewrite buffer created
2019-07-11 11:31:47 2012 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-11 11:31:47 2012 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-07-11 11:31:47 2012 [Note] InnoDB: Foreign key constraint system tables created
2019-07-11 11:31:47 2012 [Note] InnoDB: Creating tablespace and datafile system tables.
2019-07-11 11:31:47 2012 [Note] InnoDB: Tablespace and datafile system tables created.
2019-07-11 11:31:47 2012 [Note] InnoDB: Waiting for purge to start
2019-07-11 11:31:47 2012 [Note] InnoDB: 5.6.43 started; log sequence number 0
2019-07-11 11:31:47 2012 [Note] Binlog end
2019-07-11 11:31:47 2012 [Note] InnoDB: FTS optimize thread exiting.
2019-07-11 11:31:47 2012 [Note] InnoDB: Starting shutdown...
2019-07-11 11:31:49 2012 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2019-07-11 11:31:49 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-11 11:31:49 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-07-11 11:31:49 0 [Note] ./bin/mysqld (mysqld 5.6.43) starting as process 2034 ...
2019-07-11 11:31:49 2034 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-07-11 11:31:49 2034 [Note] InnoDB: The InnoDB memory heap is disabled
2019-07-11 11:31:49 2034 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-07-11 11:31:49 2034 [Note] InnoDB: Memory barrier is not used
2019-07-11 11:31:49 2034 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-07-11 11:31:49 2034 [Note] InnoDB: Using Linux native AIO
2019-07-11 11:31:49 2034 [Note] InnoDB: Using CPU crc32 instructions
2019-07-11 11:31:49 2034 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-07-11 11:31:49 2034 [Note] InnoDB: Completed initialization of buffer pool
2019-07-11 11:31:49 2034 [Note] InnoDB: Highest supported file format is Barracuda.
2019-07-11 11:31:49 2034 [Note] InnoDB: 128 rollback segment(s) are active.
2019-07-11 11:31:49 2034 [Note] InnoDB: Waiting for purge to start
2019-07-11 11:31:49 2034 [Note] InnoDB: 5.6.43 started; log sequence number 1625977
2019-07-11 11:31:50 2034 [Note] Binlog end
2019-07-11 11:31:50 2034 [Note] InnoDB: FTS optimize thread exiting.
2019-07-11 11:31:50 2034 [Note] InnoDB: Starting shutdown...
2019-07-11 11:31:51 2034 [Note] InnoDB: Shutdown completed; log sequence number 1625987
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 MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
Alternatively you can run:
./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 manual for more instructions.
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.
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
那我们怎么知道有没有成功呢。查看有没有两个 OK。或者直接 echo $? 是不是0
[root@localhost mysql]# echo $? 务必在执行完上一条命令之后马上做。这个命令是检验上一条步骤是不是对的
0
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vi /etc/init.d/mysqld
【mysql】前面的#去掉
basedir=/usr/local/mysql
datadir=/data/mysql
port = 3306
server_id = 133
[root@localhost mysql]# ls -l !$ 把它chmod成755,当然默认就是755
chmod 755 /etc/init.d/mysql
ls -l /etc/init.d/mysqld
-rwxr-xr-x. 1 root root 10644 7月 11 13:26 /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld 可以让他开机启动
[root@localhost 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@localhost mysql]# service mysqld start 可以把它启动起来
/etc/init.d/mysqld: line 33: [mysqld]: command not found
/etc/init.d/mysqld: line 48: port: command not found
/etc/init.d/mysqld: line 49: server_id: command not found
/etc/init.d/mysqld: line 50: socket: command not found
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
. SUCCESS!
Starting MySQL SUCCESS! SUCCESS说明启动成功
[root@localhost mysql]# 180727 23:28:20 mysqld_safe A mysqld process already exists
^C
[root@localhost mysql]# ps aux | grep mysql 看一下进程列表
root 2154 0.0 0.1 11812 1632 pts/0 S 13:29 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql 2250 1.2 45.0 1302748 449820 pts/0 Sl 13:29 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --usermysql --log-error=localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid
root 2276 0.0 0.0 112724 992 pts/0 S+ 13:31 0:00 grep --color=auto mysql
[root@localhost mysql]# 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 895/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1041/master
tcp6 0 0 :::3306 :::* LISTEN 2250/mysqld
tcp6 0 0 :::22 :::* LISTEN 895/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1041/master