【CentOS 7LAMP架構1】,LAMP模式介紹與MySQL安裝與配置#

shallow丿ovehtml


LAMP架構模式

Linux+Apache(httpd)+MySQL+PHPmysql

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、源碼、二進制免編譯
  • cd /usr/local/ssrc
  • wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
  • tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
  • mv mysql-5.6.36-linux-glibc2.5-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
  • etc/init.d/mysqld start

下載mysql

[root@localhost src]# uname -a	#根據系統選擇相對應版本
	Linux localhost.localdomain 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# cd /usr/local/src

[root@localhost src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
	--2017-12-14 11:07:36--  http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
	Resolving mirrors.sohu.com (mirrors.sohu.com)... 221.236.12.140
	Connecting to mirrors.sohu.com (mirrors.sohu.com)|221.236.12.140|:80... connected.
	HTTP request sent, awaiting response... 200 OK
	Length: 316320366 (302M) [application/octet-stream]
	Saving to: ‘mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz’

	100%[=========================================================>] 316,320,366  506KB/s   in 15m 24s

	2017-12-14 11:23:00 (334 KB/s) - ‘mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz’ saved [316320366/316320366]
[root@localhost src]# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
	#拆包解壓內容
	.
	.
	.
[root@localhost src]# mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql
[root@localhost src]# cd /usr/local/mysql
[root@localhost mysql]# ls
	bin      data  include  man         README   share      support-files
	COPYING  docs  lib      mysql-test  scripts  sql-bench
[root@localhost mysql]# mkdir /data/
[root@localhost mysql]# ls /data/

編譯

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2017-12-14 11:34:42 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-14 11:34:42 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-12-14 11:34:42 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 4733 ...
2017-12-14 11:34:42 4733 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-12-14 11:34:42 4733 [Note] InnoDB: The InnoDB memory heap is disabled
2017-12-14 11:34:42 4733 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-12-14 11:34:42 4733 [Note] InnoDB: Memory barrier is not used
2017-12-14 11:34:42 4733 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-12-14 11:34:42 4733 [Note] InnoDB: Using Linux native AIO
2017-12-14 11:34:42 4733 [Note] InnoDB: Using CPU crc32 instructions
2017-12-14 11:34:42 4733 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-12-14 11:34:43 4733 [Note] InnoDB: Completed initialization of buffer pool
2017-12-14 11:34:43 4733 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2017-12-14 11:34:43 4733 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2017-12-14 11:34:43 4733 [Note] InnoDB: Database physically writes the file full: wait...
2017-12-14 11:34:44 4733 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2017-12-14 11:34:46 4733 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2017-12-14 11:34:47 4733 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2017-12-14 11:34:47 4733 [Warning] InnoDB: New log files created, LSN=45781
2017-12-14 11:34:47 4733 [Note] InnoDB: Doublewrite buffer not found: creating new
2017-12-14 11:34:47 4733 [Note] InnoDB: Doublewrite buffer created
2017-12-14 11:34:47 4733 [Note] InnoDB: 128 rollback segment(s) are active.
2017-12-14 11:34:47 4733 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-12-14 11:34:47 4733 [Note] InnoDB: Foreign key constraint system tables created
2017-12-14 11:34:47 4733 [Note] InnoDB: Creating tablespace and datafile system tables.
2017-12-14 11:34:47 4733 [Note] InnoDB: Tablespace and datafile system tables created.
2017-12-14 11:34:47 4733 [Note] InnoDB: Waiting for purge to start
2017-12-14 11:34:47 4733 [Note] InnoDB: 5.6.36 started; log sequence number 0
2017-12-14 11:34:47 4733 [Note] Binlog end
2017-12-14 11:34:47 4733 [Note] InnoDB: FTS optimize thread exiting.
2017-12-14 11:34:47 4733 [Note] InnoDB: Starting shutdown...
2017-12-14 11:34:49 4733 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK

Filling help tables...2017-12-14 11:34:49 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-12-14 11:34:49 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-12-14 11:34:49 0 [Note] ./bin/mysqld (mysqld 5.6.36) starting as process 4755 ...
2017-12-14 11:34:49 4755 [Note] InnoDB: Using atomics to ref count buffer pool pages
2017-12-14 11:34:49 4755 [Note] InnoDB: The InnoDB memory heap is disabled
2017-12-14 11:34:49 4755 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2017-12-14 11:34:49 4755 [Note] InnoDB: Memory barrier is not used
2017-12-14 11:34:49 4755 [Note] InnoDB: Compressed tables use zlib 1.2.3
2017-12-14 11:34:49 4755 [Note] InnoDB: Using Linux native AIO
2017-12-14 11:34:49 4755 [Note] InnoDB: Using CPU crc32 instructions
2017-12-14 11:34:49 4755 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2017-12-14 11:34:49 4755 [Note] InnoDB: Completed initialization of buffer pool
2017-12-14 11:34:49 4755 [Note] InnoDB: Highest supported file format is Barracuda.
2017-12-14 11:34:49 4755 [Note] InnoDB: 128 rollback segment(s) are active.
2017-12-14 11:34:49 4755 [Note] InnoDB: Waiting for purge to start
2017-12-14 11:34:49 4755 [Note] InnoDB: 5.6.36 started; log sequence number 1625977
2017-12-14 11:34:49 4755 [Note] Binlog end
2017-12-14 11:34:49 4755 [Note] InnoDB: FTS optimize thread exiting.
2017-12-14 11:34:49 4755 [Note] InnoDB: Starting shutdown...
2017-12-14 11:34:51 4755 [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

New default config file was created as ./my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

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@localhost mysql]# echo $?
	0

報錯,缺乏模塊linux

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
	FATAL ERROR: please install the Perl modules before executing ./scripts/mysql_install_db:
	Data::Dumper
[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
	Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: lib
	aio.so.1: cannot open shared object file: No such file or directory

相似於以上問題,使用模糊搜索查找模塊web

[root@localhost mysql]# yum list | grep perl | grep -i dumper
	perl-Data-Dumper.x86_64                 2.145-3.el7                    @base    
	perl-Data-Dumper-Concise.noarch         2.020-6.el7                    epel     
	perl-Data-Dumper-Names.noarch           0.03-17.el7                    epel     
	perl-XML-Dumper.noarch                  0.81-17.el7                    base 
[root@localhost mysql]# yum install -y perl-Data-Dumper
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.sohu.com
Resolving Dependencies
--> Running transaction check
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be reinstalled
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================
 Package                     Arch              Version                   Repository       Size
===============================================================================================
Reinstalling:
 perl-Data-Dumper            x86_64            2.145-3.el7               base             47 k

Transaction Summary
===============================================================================================
Reinstall  1 Package

Total download size: 47 k
Installed size: 97 k
Downloading packages:
perl-Data-Dumper-2.145-3.el7.x86_64.rpm                                 |  47 kB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : perl-Data-Dumper-2.145-3.el7.x86_64                                         1/1 
  Verifying  : perl-Data-Dumper-2.145-3.el7.x86_64                                         1/1 

Installed:
  perl-Data-Dumper.x86_64 0:2.145-3.el7                                                        

Complete!
[root@localhost mysql]#  yum install libaio-devel
[root@localhost mysql]# ls support-files/
binary-configure  magic  my-default.cnf  mysqld_multi.server  mysql-log-rotate  mysql.server

配置MySQL文件

[root@localhost mysql]# vi support-files/my-default.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

"support-files/my-default.cnf" 31L, 1126C
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@localhost mysql]# cp support-files/my-default.cnf /etc/my.cnf
	cp: overwrite ‘/etc/my.cnf’? ^C
[root@localhost mysql]# ls /etc/my.cnf
[root@localhost mysql]# rpm -qf /etc/my.cnf
	mariadb-libs-5.5.56-2.el7.x86_64

也能夠直接修改/etc/my.cnfsql

[root@localhost mysql]# vi /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/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=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

將第2行的datadir=/var/lib/mysql改成datadir=/data/mysql 第3行的socket=/var/lib/mysql/mysql.sock改成socket=/tmp/mysql.sock 能夠將第十二、1三、18行加上註釋符號,暫且不用 #log-error=/var/log/mariadb/mariadb.log #pid-file=/var/run/mariadb/mariadb.pid #!includedir /etc/my.cnf.d數據庫

自定義啓動腳本

[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vi +46 /etc/init.d/mysqld
	.
	.
	.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=
datadir=

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.
# 0 means don't wait at all
# Negative numbers mean to wait indefinitely
service_startup_timeout=900

	.
	.
	.

"/etc/init.d/mysqld" 378L, 10565C

basedir=/usr/local/mysql datadir=/data/mysqlbootstrap

[root@localhost mysql]# chmod 755 /etc/init.d/mysqld 
[root@localhost mysql]# ls -l !$
	ls -l /etc/init.d/mysqld
	-rwxr-xr-x 1 root root 10592 Dec 14 12:26 /etc/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# 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]'.

iprdump        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprinit        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
iprupdate      	0:off	1:off	2:on	3:on	4:on	5:on	6:off
mysql-proxy    	0:off	1:off	2:off	3:off	4:off	5:off	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

啓動mysql

[root@localhost mysql]# /etc/init.d/mysqld start
	或service mysqld start

[root@localhost mysql]# service mysqld start
	Starting MySQL. SUCCESS!
[root@localhost mysql]# ps aux | grep mysql
root       5445  0.0  0.0  11776  1596 pts/1    S    12:32   0:00 /bin/sh /usr/local/mysql/binmysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/localhost.localdomain.pid
mysql      5583  1.5 24.3 1300784 454860 pts/1  Sl   12:32   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/localhost.localdomain.err --pid-file=/data/mysql/localhost.localdomain.pid --socket=/tmp/mysql.sock
root       5607  0.0  0.0 112660   972 pts/1    S+   12:33   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 127.0.0.1:25            0.0.0.0:*               LISTEN      2470/master         
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:52133           0.0.0.0:*               LISTEN      1496/rpc.statd      
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1951/rpc.mountd     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1406/sshd           
tcp        0      0 0.0.0.0:37559           0.0.0.0:*               LISTEN      -                   
tcp6       0      0 ::1:25                  :::*                    LISTEN      2470/master         
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
tcp6       0      0 :::48771                :::*                    LISTEN      -                   
tcp6       0      0 :::46184                :::*                    LISTEN      1496/rpc.statd      
tcp6       0      0 :::3306                 :::*                    LISTEN      5583/mysqld         
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::20048                :::*                    LISTEN      1951/rpc.mountd     
tcp6       0      0 :::22                   :::*                    LISTEN      1406/sshd

mysql端口號3306架構

使用命令行方式啓動mysqlapp

[root@localhost mysql]# service mysqld stop
	Shutting down MySQL.. SUCCESS! 
[root@localhost mysql]# !ps
	ps aux | grep mysql
	root       5635  0.0  0.0 112660   976 pts/1    S+   12:35   0:00 grep --color=auto mysql
[root@localhost mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf  --user=mysql --datadir=/data/mysql &
	[1] 5636
[root@localhost mysql]# 171214 12:38:08 mysqld_safe Logging to '/data/mysql/localhost.localdomain.err'.
	171214 12:38:08 mysqld_safe Starting mysqld daemon with databases from /data/mysql

[root@localhost mysql]# !ps
	ps aux | grep mysql
	root       5636  0.0  0.0 113264  1612 pts/1    S    12:38   0:00 /bin/sh /usr/local/mysql/binmysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
	mysql      5761  2.5 24.0 1300784 449668 pts/1  Sl   12:38   0:00 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --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       5784  0.0  0.0 112660   972 pts/1    S+   12:38   0:00 grep --color=auto mysql
[root@localhost mysql]# !net
	netstat -lntp
	Active Internet connections (only servers)
	Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
	tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2470/master         
	tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
	tcp        0      0 0.0.0.0:52133           0.0.0.0:*               LISTEN      1496/rpc.statd      
	tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
	tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      1951/rpc.mountd     
	tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1406/sshd           
	tcp        0      0 0.0.0.0:37559           0.0.0.0:*               LISTEN      -                   
	tcp6       0      0 ::1:25                  :::*                    LISTEN      2470/master         
	tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
	tcp6       0      0 :::48771                :::*                    LISTEN      -                   
	tcp6       0      0 :::46184                :::*                    LISTEN      1496/rpc.statd      
	tcp6       0      0 :::3306                 :::*                    LISTEN      5761/mysqld         
	tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
	tcp6       0      0 :::20048                :::*                    LISTEN      1951/rpc.mountd     
	tcp6       0      0 :::22                   :::*                    LISTEN      1406/sshd

關閉mysql

[root@localhost mysql]# /etc/init.d/mysqld stop
[root@localhost mysql]# service mysqld stop

而命令只能kallall service關閉服務或指定kill piddom

[root@localhost mysql]# killall mysqld
[root@localhost mysql]# 171214 12:42:59 mysqld_safe mysqld from pid file /data/mysql/localhost.localdomain.pid ended

	[1]+  Done                    /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
[root@localhost mysql]# !ps
	ps aux | grep mysql
	root       5792  0.0  0.0 112660   976 pts/1    S+   12:43   0:00 grep --color=auto mysql

若關閉mysql時發現kill不掉時,這時候頗有可能數據量大的mysql正在寫入磁盤,不要輕易使用kill -9,不然會損壞mysql表

其它

  • mysql的引擎

innodb和myisam

相關文章
相關標籤/搜索