1.1 LAMP架构介绍
1.2 MySQL/Mariadb介绍
- MySQL是一个关系型数据库,由mysql ab公司开发,mysql在2008年被sun公司收购(10亿刀),2009年sun公司被oracle公司收购(74亿刀)
- MySQL官网 最新版本5.7GA/8.0DMR
- MySQL5.6变化比较大,5.7性能上有很大提升
- Mariadb为MySQL的一个分支,官网
- 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位区分)
这里我们采用二进制免编译包进行安装:[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]# lsmysql-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]# lsmysql-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/mysqlInstalling 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/mysqlWARNING: 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 systemThis file will be read by default by the MySQL serverIf 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命令直接本地上传了哦;
[root@Dasoncheng src]# yum install -y lrzsz[root@Dasoncheng src]# rz [root@Dasoncheng src]# lsmysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
编辑配置文件:
[root@Dasoncheng ~]# mv /etc/my.cnf /etc/my.cnf.bak[root@Dasoncheng mysql]# lsbin data include man my-new.cnf README share support-filesCOPYING 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/mysqlsocket = /tmp/mysql.sock
编辑启动脚本:
[root@Dasoncheng ~]# cp support-files/mysql.server /etc/init.d/mysqld[root@Dasoncheng ~]# vim /etc/init.d/mysqldbasedir=/usr/local/mysqldatadir=/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.pidmysql 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.sockroot 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 --listNote: 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:offnetconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:offnetwork 0:off 1:off 2:on 3:on 4:on 5:on 6:off
假如:有一天 你没有启动脚本到/etc/init.d目录下、或者没有模版等等。你可以尝试命令行启动:
[root@Dasoncheng ~]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &##指定配置文件、指定启动用户 和 指定数据库目录;
那么命令行启动了 怎么stop呢?
[root@Dasoncheng mysql]# yum install -y psmisc[root@Dasoncheng ~]# killall mysqld[root@Dasoncheng ~]# ps aux |grep mysqlroot 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安装:
- cd /usr/local/src
- wget
- tar zxvf mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
- mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb
- cd /usr/local/mariadb
- ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mariadb/ --datadir=/data/mariadb
- cp support-files/my-small.cnf /usr/local/mariadb/my.cnf vi /usr/local/mariadb/my.cnf //定义datadir
- cp support-files/mysql.server /etc/init.d/mariadb vim /etc/init.d/mariadb //定义basedir、datadir、conf以及启动参数
- /etc/init.d/mariadb start
[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]# lsmariadb-10.2.6-linux-glibc_214-x86_64mariadb-10.2.6-linux-glibc_214-x86_64.tar.gzmysql-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]# lsbin data include mysql-test shareCOPYING DESTINATION INSTALL-BINARY README.md sql-benchCOPYING.thirdparty docs lib README-wsrep support-filesCREDITS 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 testaria_log_control ibdata1 ib_logfile1 performance_schema[root@Dasoncheng mariadb]# cp support-files/my-my-huge.cnf my-large.cnf my-small.cnfmy-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 = 3306datadir = /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/mariadbconf=$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 startReloading systemd: [ OK ]Starting mariadb (via systemctl): [ OK ][root@Dasoncheng mariadb]# ps aux |grep mariadbroot 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.pidmysql 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=3306root 2596 0.0 0.0 112652 952 pts/0 S+ 23:40 0:00 grep --color=auto mariadb[root@Dasoncheng mariadb]# netstat -lntp |grep mysqltcp6 0 0 :::3306 :::* LISTEN 2560/mysqld ##到这里,mariadb已经安装 并启动完成了;
关于mysql的常用引擎: 第21套 第6题
1、myisam
2、innodb 后续总结:mysql5.5源码编译安装
mysql5.7二进制包安装(变化较大)