1.1 LAMP架构介绍

1.1 LAMP架构介绍

mark
mark

1.2 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、源码包、二进制包(免编译--有32/64位区分)
这里咱们采用二进制免编译包进行安装:html

[root@Dasoncheng ~]# cd /usr/local/src/    ##将下载的软件包放这里;
[root@Dasoncheng src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
-bash: wget: command not found  ##下载二进制包报错,下面安装wget
[root@localhost ~]# yum makecache    ##安装前先重置yum包信息缓存 第一次使用/修改了repo以后 重置以后会使用快一点;
[root@Dasoncheng src]# yum install -y wget
[root@Dasoncheng src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 
[root@Dasoncheng src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@Dasoncheng src]# tar -zxf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  ##解压二进制包;
[root@Dasoncheng src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64  mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@Dasoncheng src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql  ##移动并重命名为mysql;
[root@Dasoncheng src]# cd /usr/local/mysql
[root@Dasoncheng mysql]# useradd mysql -s /sbin/nologin  ##建立mysql用户,由于启动mysql须要该用户;
[root@Dasoncheng mysql]# mkdir -p /data/mysql    ##建立/data/mysql,之后数据会存放该目录下;
[root@Dasoncheng mysql]# chown -R mysql:mysql /data/mysql/    ##修改目录权限,否则后面会出问题;
[root@Dasoncheng mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
##初始化mysql,--user指定以什么用户运行、--datadir指定数据库存放的目录;
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
[root@Dasoncheng mysql]# yum install -y perl-Data-Dumper    ##安装依赖软件包;
[root@Dasoncheng mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
[root@Dasoncheng mysql]# yum install -y libaio    ##安装依赖软件包;
[root@Dasoncheng mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
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

[root@Dasoncheng mysql]# echo $?    ##安装成功;
0

由于我本地有mysql的包,因此我使用rz命令直接本地上传了哦;mysql

[root@Dasoncheng src]# yum install -y lrzsz
[root@Dasoncheng src]# rz 
[root@Dasoncheng src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz

编辑配置文件:

[root@Dasoncheng ~]# mv /etc/my.cnf /etc/my.cnf.bak
[root@Dasoncheng mysql]# ls
bin      data  include  man     my-new.cnf  README   share      support-files
COPYING  docs  lib      my.cnf  mysql-test  scripts  sql-bench
[root@Dasoncheng mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@Dasoncheng mysql]# vim /etc/my.cnf
[mysqld]
datadir = /data/mysql
socket = /tmp/mysql.sock

编辑启动脚本:

[root@Dasoncheng ~]# cp support-files/mysql.server /etc/init.d/mysqld
[root@Dasoncheng ~]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql

测试启动mysql:

[root@Dasoncheng ~]# service mysqld start
[root@Dasoncheng ~]# /etc/init.d/mysqld start    ##以上两种方法均可以启动;
Starting MySQL. SUCCESS!
[root@Dasoncheng ~]# ps aux |grep mysql    ##查看进程
root       2848  0.2  0.1 113252  1588 pts/0    S    04:36   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql      2984  5.1 45.3 973056 453100 pts/0   Sl   04:36   0:00 /usr/local/mysql/binmysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock
root       3008  0.0  0.0 112648   960 pts/0    S+   04:36   0:00 grep --color=auto mysql
[root@Dasoncheng ~]# netstat -lntp |grep mysql    ##查看使用端口;
tcp6       0      0 :::3306                 :::*                    LISTEN      2984/mysqld         
##启动成功!

添加到系统服务 开机启动:

[root@Dasoncheng ~]# chkconfig --add mysqld
[root@Dasoncheng ~]# 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]'.

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

假如:有一天 你没有启动脚本到/etc/init.d目录下、或者没有模版等等。你能够尝试命令行启动:linux

[root@Dasoncheng ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &
##指定配置文件、指定启动用户 和 指定数据库目录;

那么命令行启动了 怎么stop呢?sql

[root@Dasoncheng mysql]# yum install -y psmisc
[root@Dasoncheng ~]# killall mysqld
[root@Dasoncheng ~]# ps aux |grep mysql
root       3401  0.0  0.0 112648   960 pts/0    S+   05:14   0:00 grep --color=auto mysql

其中:killall比kill安全的多,若是直接kill进程 就可能会产生数据丢失;若是是killall的话 会先中止写读操做,而后将数据写入磁盘完成后 再杀死进程;
若是未来有一天,mysql进程始终杀不死 :说明数据量大 正在写入数据,你须要作的就是等!写完了以后 进程就会killall掉;数据库

Mariadb安装:

[root@Dasoncheng ~]# cd /usr/local/src/
[root@Dasoncheng src]# wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bintar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
[root@Dasoncheng src]# tar -zxf mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz 
[root@Dasoncheng src]# ls
mariadb-10.2.6-linux-glibc_214-x86_64
mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
[root@Dasoncheng src]# mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb
[root@Dasoncheng src]# cd !$
cd /usr/local/mariadb
[root@Dasoncheng mariadb]# ls
bin                 data               include         mysql-test    share
COPYING             DESTINATION        INSTALL-BINARY  README.md     sql-bench
COPYING.thirdparty  docs               lib             README-wsrep  support-files
CREDITS             EXCEPTIONS-CLIENT  man             scripts
[root@Dasoncheng mariadb]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mariadb --datadir=/data/mariadb
##初始化mariadb,定义用户、安装目录和数据库目录;
[root@Dasoncheng mariadb]# echo $?
0
[root@Dasoncheng mariadb]# ls /data/mariadb/    ##我没建立数据库目录,就是指定了一下 自动建立了;
aria_log.00000001  ib_buffer_pool  ib_logfile0  mysql               test
aria_log_control   ibdata1         ib_logfile1  performance_schema
[root@Dasoncheng mariadb]# cp support-files/my-
my-huge.cnf             my-large.cnf            my-small.cnf
my-innodb-heavy-4G.cnf  my-medium.cnf           
[root@Dasoncheng mariadb]# cp support-files/my-small.cnf /usr/local/mariadb/my.cnf    ##拷贝配置文件 并编辑;
[root@Dasoncheng mariadb]# vim /usr/local/mariadb/my.cnf
[mysqld]
port            = 3306
datadir         = /data/mariadb    ##mysqld模块下,添加datadir 便可;
socket          = /tmp/mysql.sock
[root@Dasoncheng mariadb]# cp support-files/mysql.server /etc/init.d/mariadb    ##拷贝启动脚本到/etc/init.d/目录下;
[root@Dasoncheng mariadb]# vim /etc/init.d/mariadb 
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mariadb   ##定义好这三个变量,下面会引用到;
datadir=/data/mariadb
conf=$basedir/my.cnf

# Default value, in seconds, afterwhich the script should timeout waiting
……
……
case "$mode" in
  'start')
    # Start daemon

    # Safeguard (relative paths, core dumps..)
    cd $basedir

    echo $echo_n "Starting MySQL"
    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" "$@" &
##这里会引用到,我只是添加了--defaults-file="$conf"
[root@Dasoncheng mariadb]# service mysqld stop    ##先看看mysql 有没有启动,启动的话中止掉;(只能一个进程占用3306端口,一台机器是能够跑多个数据库的)
Shutting down MySQL.. SUCCESS!
[root@Dasoncheng mariadb]# /etc/init.d/mariadb start
Reloading systemd:                                         [  OK  ]
Starting mariadb (via systemctl):                          [  OK  ]
[root@Dasoncheng mariadb]# ps aux |grep mariadb
root       2441  0.4  0.1 115380  1720 ?        S    23:40   0:00 /bin/sh /usr/local/mariadb/bin/mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf --datadir=/data/mariad --pid-file=/data/mariadb/localhost.localdomain.pid
mysql      2560  6.5  5.7 1583772 57900 ?       Sl   23:40   0:00 /usr/local/mariadb/bin/mysqld --defaults-file=/usr/local/mariadb/my.cnf --basedir=/usr/local/mariadb --datadir=/data/mariadb --plugin-dir=/usr/local/mariadb/lib/plugin --user=mysql --log-error=/data/mariadb/localhost.localdomain.err --pid-file=/data/mariadb/localhost.localdomain.pid --socket=/tmp/mysql.sock --port=3306
root       2596  0.0  0.0 112652   952 pts/0    S+   23:40   0:00 grep --color=auto mariadb
[root@Dasoncheng mariadb]# netstat -lntp |grep mysql
tcp6       0      0 :::3306                 :::*                    LISTEN      2560/mysqld 
##到这里,mariadb已经安装 并启动完成了;

关于mysql的经常使用引擎: 第21套 第6题

一、myisam
二、innodb
后续总结:bootstrap

mysql5.5源码编译安装 http://www.aminglinux.com/bbs/thread-1059-1-1.html
mysql5.7二进制包安装(变化较大) http://www.apelearn.com/bbs/thread-10105-1-1.htmlvim

相关文章
相关标签/搜索