LNMP环境介绍
Mariadb安装
服务管理
LNMP环境介绍
Linux+Nginx+Mysql/mariadb+php
Nginx:是一个WEB服务器,提供http服务
Mysql/MariaDB:是个关系型数据,用来存数据的(用户名、密码、文章内容)
PHP:是一个编程语言,常用来做网站(qq.com baidu.com google.com ask.apelearn.com)
Nginx是一个WEB服务器,所以用户首先访问到的就是Nginx(静态的请求,会处理图片、js、css,接收php的请求,但是不处理)把php的请求转给后面的php-fpm
php-fpm会处理php相关的请求(叫做动态的请求)
动态与静态
所谓静态,指的是Nginx可以直接处理的图片、js、css、视频、音频、flash等等
所谓动态,指的是这些请求需要和数据库打交道。比如,用户登录过程,比如查看一篇文章,或者写一篇文章
Mariadb
MariaDB是MySQL的一个分支。 MySQL --> SUN --> Oracle(最终被这家公司收购) #因为可能有mysql有闭源的风险,想Facebook大公司已放弃了mysql数据库,转向marialDB数据库。
维基百科: https://zh.wikipedia.org/wiki/MariaDB#cite_note-103_release-21
下载mariadb:
** 选择免编译版本**
** 复制链接地址**
在linux中下载到/usr/local/src下
[root@localhost src]# wget https://downloads.mariadb.org/interstitial/mariadb-10.3.11/bintar-linux-x86_64/mariadb-10.3.11-linux-x86_64.tar.gz
mariadb安装流程
解压 #建议把安装包放在如下命令
/usr/local/src/
[root@localhost src]# tar zxvf mariadb-10.3.11-linux-x86_64.tar.gz
tar zxvf XXX.tar.gz z:针对gz解压
tar jxvf XXX.tar.bz2 j:针对bz2解压
tar Jxvf XXX.tar.xz J:针对xz解压
压缩 #拓展命令
tar zxvf XXX.tar.gz XXX/
tar jxvf XXX.tar.bz2 XXX/
tar Jxvf XXX.tar.xz XXX/
移动并改名 /usr/local/mysql #以后工作建议把安装程序放在这
[root@localhost src]# mv mariadb-10.3.11-linux-x86_64 /usr/local/mysql
创建目录 及账号
[root@localhost mysql]# mkdir -p /data/mysql
[root@localhost mysql]# useradd -M -s /sbin/nologin mysql
[root@localhost mysql]# grep mysql /etc/passwd
mysql:x:1000:1000::/home/mysql:/sbin/nologin
[root@localhost mysql]# chown -R mysql:mysql /data/mysql
初始化设置
[root@localhost mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --usr=mysql
第一次安装可能会有如下错误
error while loading shared libraries: libaio.so.1: cannot open shared object file:
No such file or directory
解决: yum install -y libaio libaio-devel
#注意哦!安装好上面套件后,注意再次输入初始化设置命令!
验证是否成功 输出是0是正确的 不正确的情况显示的是1
[root@localhost mysql]# echo $? #对前面一条命令验证是否成功
0
拷贝启动脚本
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
编辑启动脚本
[root@localhost mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
启动服务
[root@localhost mysql]# /etc/init.d/mysqld start
24. 服务管理
列出系统所有的服务
chkconfig --list Centos6 (cento7版本也可用)
systemctl list-unit-files centos7
chkconfig 增加服务
/etc/init.d/下有mysqld 并且权限是755 (前提,这样才能增加服务)
[root@localhost init.d]# pwd
/etc/init.d
[root@localhost init.d]# ll |grep mys
-rwxr-xr-x. 1 root root 12193 Jan 24 00:39 mysqld
[root@localhost mysql]# chkconfig --add mysqld
开机启动服务
chkconfig mysqld off
chkconfig mysqld on
# 25. Mariadb安装(下)
## 修改配置文件my.cnf
[root@localhost init.d]# vi /etc/my.cnf
datadir=/data/mysql socket=/tmp/mysql.sock log-error=/data/mysql/mariadb.log pid-file=/data/mysql/mariadb.pid
启动服务
/etc/init.d/mysqld start == service mysqld start
启动成功如下
[root@localhost mysql]# service mysqld start Starting mysqld (via systemctl): [ OK ]
用ps aux|grep mysql 或netstat -lnp查看监听端口是否开启服务 #3306mariadb端口
接着连接数据库,数据库加密码。远程登录其他服务器, 系统环境变量PATH: echo $PATH
PATH的作用:可以直接用PATH这些路径里面的文件,不用敲绝对路径了(临时有效)。
[root@test01 mysql]# ps aux |grep mysql 查看一个进程查看一个服务
root 3159 0.0 0.1 115380 1732 ? S 16:28 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/test01.pid
mysql 3244 0.2 8.4 1254884 84544 ? Sl 16: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 3279 0.0 0.0 112664 968 pts/0 S+ 16:30 0:00 grep --color=auto mysql
[root@test01 mysql]# netstat -lnp 查看监听端口,查看有没有3306端口
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 973/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1253/master
tcp6 0 0 :::3306 :::* LISTEN 3244/mysqld
tcp6 0 0 :::22 :::* LISTEN 973/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1253/master
udp 0 0 127.0.0.1:323 0.0.0.0:* 630/chronyd
udp6 0 0 ::1:323 :::* 630/chronyd
raw6 0 0 :::58 :::* 7 670/NetworkManager
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node PID/Program name Path
unix 2 [ ACC ] SEQPACKET LISTENING 12858 1/systemd /run/udev/control
unix 2 [ ACC ] STREAM LISTENING 14957 1/systemd /var/run/dbus/system_bus_socket
unix 2 [ ACC ] STREAM LISTENING 19060 1253/master private/defer
unix 2 [ ACC ] STREAM LISTENING 19063 1253/master private/trace
unix 2 [ ACC ] STREAM LISTENING 19066 1253/master private/verify
unix 2 [ ACC ] STREAM LISTENING 19073 1253/master private/proxymap
unix 2 [ ACC ] STREAM LISTENING 19076 1253/master private/proxywrite
unix 2 [ ACC ] STREAM LISTENING 19079 1253/master private/smtp
unix 2 [ ACC ] STREAM LISTENING 19082 1253/master private/relay
unix 2 [ ACC ] STREAM LISTENING 19088 1253/master private/error
unix 2 [ ACC ] STREAM LISTENING 19053 1253/master private/rewrite
unix 2 [ ACC ] STREAM LISTENING 19091 1253/master private/retry
unix 2 [ ACC ] STREAM LISTENING 19094 1253/master private/discard
unix 2 [ ACC ] STREAM LISTENING 19097 1253/master private/local
unix 2 [ ACC ] STREAM LISTENING 19100 1253/master private/virtual
unix 2 [ ACC ] STREAM LISTENING 19103 1253/master private/lmtp
unix 2 [ ACC ] STREAM LISTENING 19106 1253/master private/anvil
unix 2 [ ACC ] STREAM LISTENING 19109 1253/master private/scache
unix 2 [ ACC ] STREAM LISTENING 19045 1253/master public/qmgr
unix 2 [ ACC ] STREAM LISTENING 19056 1253/master private/bounce
unix 2 [ ACC ] STREAM LISTENING 19069 1253/master public/flush
unix 2 [ ACC ] STREAM LISTENING 19085 1253/master public/showq
unix 2 [ ACC ] STREAM LISTENING 19050 1253/master private/tlsmgr
unix 2 [ ACC ] STREAM LISTENING 30687 3244/mysqld /tmp/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 19038 1253/master public/pickup
unix 2 [ ACC ] STREAM LISTENING 19042 1253/master public/cleanup
unix 2 [ ACC ] STREAM LISTENING 8405 1/systemd /run/systemd/journal/stdout
unix 2 [ ACC ] STREAM LISTENING 12784 1/systemd /run/systemd/private
[root@test01 mysql]# ls -l /tmp/
总用量 20
lrwxrwxrwx. 1 root root 5 1月 23 22:42 333.txt -> 1.txt
lrwxrwxrwx. 1 root root 11 1月 23 22:48 44.txt -> /root/1.txt
-rw-r--r--. 1 root root 1572 1月 16 20:09 CentOS7-Base-163.repo123
-rw-r--r--. 1 root root 516 1月 18 17:18 inittab
-rwx------. 1 root root 836 1月 17 00:03 ks-script-fPPgkL
srwxrwxrwx. 1 mysql mysql 0 1月 24 16:28 mysql.sock
-rw-r--r--. 1 root root 1044 1月 18 17:27 passwd.txt
drwx------. 3 root root 17 1月 18 15:10 systemd-private-32ccfac892ba43a891ba41d70c22bae1-vmtools
drwx------. 3 root root 17 1月 24 14:22 systemd-private-bf6f7e606d70439da08fad1ca2735245-vmtools
drwx------. 3 root root 17 1月 22 12:10 systemd-private-f6f758ff9d8f483486f4d6e25fabe148-vmtools
-rw-------. 1 root root 0 1月 16 23:52 yum.log
-rw-r--r--. 1 root root 385 1月 17 20:31 yu.txt
[root@test01 mysql]# ls -l /tmp/mysql.sock
srwxrwxrwx. 1 mysql mysql 0 1月 24 16:28 /tmp/mysql.sock
连接mySQL/mariaDB
[root@test01 mysql]# /usr/local/mysql/bin/mysql -uroot
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.
MariaDB [(none)]> Bye
定义变量直接登录mysql
[root@test01 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@test01 mysql]# PATH=$PATH:/usr/local/mysql/bin
[root@test01 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@test01 mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@test01 mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@test01 mysql]# source /etc/profile
[root@test01 mysql]# mysql -uroot 可以直接用PATH这些路径里面的文件,不用敲绝对路径了。
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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.
MariaDB [(none)]>
[root@test01 mysql]# mysqladmin -uroot password "chamlinux"
[root@test01 mysql]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@test01 mysql]# mysql -uroot -pchamlinux
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
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.
MariaDB [(none)]>
[root@test01 mysql]# mysql -uroot -pchamlinux -S/tmp/mysql.sock 指定sock登录
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 14
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.
MariaDB [(none)]>
连接远程其他服务器数据库
[root@test01 mysql]# mysql -uroot -pchamlinux -h192.168.28.108 -P3306