hellopasswd
LAMP架构模式
Linux+Apache(httpd)+MySQL+PHP
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
报错,缺少模块
[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
类似于以上问题,使用模糊搜索查找模块
[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.cnf
[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 可以将第12、13、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/mysql
[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
使用命令行方式启动mysql
[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 pid
[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
修改于 171214