搭建LNMP(一)MySQL/MariaDB

LNMP 架构介绍

  • LNMP==Linux+Nginx+Mysql+PHP
  • 和LAMP不一样的是,提供web服务的是Nginx 而且php是做为一个独立服务存在的,这个服务叫作php-fpm Nginx直接处理静态请求,动态请求会转发给php-fpm

MySQL & MariaDB

甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,所以社区采用分支的方式来避开这个风险。 过去一年中,大型互联网用户以及Linux发行商纷纷抛弃MySQL,转投MariaDB阵营。MariaDB是目前最受关注的MySQL数据库衍生版,也被视为开源数据库MySQL的替代品。php

安装MariaDB

  • 在服务器上进入/usr/local/src目录下,使用wget命令来下载MariaDB的二进制包。咱们会将全部的须要手动下载的软件包都放在/usr/local/src目录下。
[root@test01 ~]# cd /usr/local/src
[root@test01 src]# wget http://mirrors.neusoft.edu.cn/mariadb//mariadb-10.3.12/bintar-linux-x86_64/mariadb-10.3.12-linux-x86_64.tar.gz
  • 将压缩包解压缩,将解压缩的目录移动到/usr/local/下更名mysql。
[root@test01 src]# tar -zxvf mariadb-10.3.12-linux-x86_64.tar.gz
[root@test01 src]# ls
mariadb-10.3.12-linux-x86_64  mariadb-10.3.12-linux-x86_64.tar.gz
[root@test01 src]# mv mariadb-10.3.12-linux-x86_64 /usr/local/mysql
[root@test01 src]# ls /usr/local/mysql/
bin  COPYING  COPYING.thirdparty  CREDITS  data  docs  EXCEPTIONS-CLIENT  include  INSTALL-BINARY  lib  man  mysql-test  README.md  README-wsrep  scripts  share  sql-bench  support-files
  • 须要为MariaDB建立一个数据库的目录,还须要为MariaDB建立一个用户,该用户能够不给登陆的权限。
[root@test01 src]# mkdir -p /data/mysql                       #为MariaDB建立目录
[root@test01 src]# useradd -M -s /sbin/nologin mysql          #为MariaDB建立用户

上面所示的useradd -M 表示不为该用户建立家目录,-s是指定shell的,后面的nologin表示是系统用户,不能进行登陆操做。mysql

  • 进入/usr/local/mysql目录执行初始化命令
[root@test01 mysql]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql/' ...
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
  • 出现上面这个报错时,能够安装libaio和libaio-devel两个包来解决。而后再初始化,初始化完成后不知道初始化的过程有没有问题,初始化是否成功,可使用echo $? 来验证,若是输出结果是0,则表示没问题,若是是非0的则须要查看是出现了什么问题。
[root@test01 mysql]# yum install libaio libaio-devel -y
[root@test01 mysql]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
[root@test01 mysql]# echo $?
0
  • 复制启动文件模板,并按实际状况编辑启动文件
[root@test01 support-files]# cp mysql.server /etc/init.d/mysqld
[root@test01 support-files]# vim /etc/init.d/mysqld
  • 将启动文件中的下面两行按本身的实际状况修改。
basedir=/usr/local/mysql
datadir=/data/mysql
  • 将mysqld服务添加到chkconfig(服务管理)中
[root@test01 ~]# chkconfig --add mysqld
[root@test01 ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

agentwatch     	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysqld         	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
  • 编辑配置文件
[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=/data/mysql/mariadb.log					须要修改的地方
pid-file=/data/mysql/mariadb.pid					须要修改的地方

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
  • 启动mysqld服务。
[root@test01 ~]# ps aux | grep mysql
root      2433  0.0  0.1 112708   980 pts/0    S+   13:23   0:00 grep --color=auto mysql               这个状态是没启动的
[root@test01 ~]# service mysql start
Starting MariaDB.190209 13:28:36 mysqld_safe Logging to '/data/mysql/mariadb.log'.
190209 13:28:36 mysqld_safe Starting mysqld daemon with databases from /data/mysql
                                                           [  OK  ]
[root@test01 ~]# ps aux | grep mysql
root      2782  0.0  0.3  11816  1640 pts/0    S    13:28   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/test01.pid
mysql     2870  1.3 14.7 1255904 73784 pts/0   Sl   13:28   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=/data/mysql/mariadb.log --pid-file=/data/mysql/test01.pid --socket=/tmp/mysql.sock
root      2911  0.0  0.1 112708   980 pts/0    R+   13:28   0:00 grep --color=auto mysql                                  这个状态是启动的状态
  • 还能够经过查看监听的端口来肯定服务是否启动
[root@test01 ~]# 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      3067/sshd           
tcp6       0      0 :::3306                 :::*                    LISTEN      2870/mysqld
  • MariaDB的链接
[root@test01 ~]# /usr/local/mysql/bin/mysql -uroot                    使用root来链接
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.12-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  • 上面命令中的/usr/local/mysql/bin/mysql命令必须使用绝对路径,比较长,为了方便使用,咱们能够经过作软连接、alias或更改PATH来将这个命令简化。
[root@test01 bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/     软连接的作式
[root@test01 bin]# alias mysql='/usr/local/mysql/bin/mysql'       作别名的方式、
[root@test01 ~]# PATH=$PATH:/usr/local/mysql/bin/                 更改环境变量(临时生效,换终端不生效)
更改/etc/profile 文件,在最后添加一行export PATH=$PATH:/usr/local/mysql/bin 永久生效。
  • 设定链接mysql 的密码,并使用密码链接
[root@test01 ~]# mysqladmin -uroot password "mysqlpasscode"           设定密码
[root@test01 ~]# mysql -uroot -pmysqlpasscode                         使用密码链接。
相关文章
相关标签/搜索