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

相關文章
相關標籤/搜索