11.1 LAMP架构介绍 11.2 MySQL、MariaDB介绍 11.3/11.4/11.5 MySQL安装
# 11.3 MySQL 安装 (上)- MySQL的几个常用安装包:rpm、源码、二进制免编译
- 二进制免编译(发布之前在linux服务器上租了一个编译,编译完了之后,把编译完成的文件重新安排,放到一个目录下去,然后打包压缩,发布)有一个好处,不用花那么多时间去配置,直接拿来用就可以
- rpm 包有一个缺点,没有办法去定义你所安装的路径,默认就安装在/usr下
- 二进制免编译包可以放在一个目录下,比如说/urs/local/src下,也可以放在别的目录下,随便你自己
- 二进制免编译包毕竟在其他编辑器上编辑的,如果想追求极致的性能,就自己去编译
- 如果工作中没有特殊的要求,可以用二进制免编译包就可以。
- 先进入到目录 /usr/local/src
```
# cd /usr/local/src/
# ls
httpd-2.4.27httpd-2.4.27.tar.gz
```
- 用命令uname -a 查看当前系统版本,x86_64 这个是64位的
- 可以去r.aminglinux.com 下载地址 ,我们这边下载5.6_64位二进制包
```
# uname -a
Linux aminglinux-001 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
#
```
- 使用wget下载
```
# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2
--2017-09-19 23:06:16--http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_
正在解析主机 mirrors.sohu.com (mirrors.sohu.com)... 221.236.12.140
正在连接 mirrors.sohu.com (mirrors.sohu.com)|221.236.12.140|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:314581668 (300M)
正在保存至: “mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz”
12% [============> 100%[======================================================>] 314,581,668486KB/s 用时 18m 3s
2017-09-19 23:24:19 (284 KB/s) - 已保存 “mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz” )
#
```
# 11.4 MySQL 安装 (中)
- 下载完之后第一步是要解压,
```
# ls
httpd-2.4.27httpd-2.4.27.tar.gzmysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
.
.
.
mysql-5.6.35-linux-glibc2.5-x86_64/mysql-test/include/stop_slave.inc
mysql-5.6.35-linux-glibc2.5-x86_64/mysql-test/mysql-test-run.pl
mysql-5.6.35-linux-glibc2.5-x86_64/mysql-test/purify.supp
mysql-5.6.35-linux-glibc2.5-x86_64/mysql-test/valgrind.supp
```
- 挪目录到local目录下 并且改名mysql(mysql也是目录), mv /usr/local/mysql,然后到mysql目录下
```
# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
# cd /usr/local/mysql/
# ls
bin dataincludeman README share support-files
COPYINGdocslib mysql-testscriptssql-bench
#
```
- 创建mysql用户,创建目录/data/
```
# useradd mysql
# mkdir /data/
mkdir: 无法创建目录"/data/": 文件已存在
# ls /data/
liurongluan
#
```
- 运行./scripts/mysql_install_db --user=mysql --datadir=/data/mysql 初始化
```
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
#
```
- 现在报错了, please install the following Perl modules before executing 提示少了一个perl模块,名字是Dumper,尝试搜索一下
```
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
# 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
#
```
- 安装第四个试下 perl-XML-Dumper
```
# yum install -y perl-XML-Dumper
已加载插件:fastestmirror
base | 3.6 kB00:00:00
epel/x86_64/metalink
已安装:
perl-XML-Dumper.noarch 0:0.81-17.el7
作为依赖被安装:
perl-XML-Parser.x86_64 0:2.41-10.el7
完毕!
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
#
```
- 还是不行,再试下第一个包 perl-Data-Dumper
```
# yum install -y perl-Data-Dumper
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* epel: mirrors.tuna.tsinghua.edu.cn
已安装:
perl-Data-Dumper.x86_64 0:2.145-3.el7
完毕!
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables...2017-09-23 13:01:36 0 TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-09-23 13:01:36 0 Ignoring --secure-file-priv value as server is running with --bootstrap.
2017-09-23 13:01:36 0 ./bin/mysqld (mysqld 5.6.35) starting as process 2630 ...
2017-09-23 13:01:36 2630 InnoDB: Using atomics to ref count buffer pool pages
2017-09-23 13:01:36 2630 InnoDB: The InnoDB memory heap is disabled
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
#
```
-可以了,怎么查看一个命令执行完后 是否正确? 再上一个命令运行完之后 echo $? 结果是0 就是正确的,是1就是错误的
```
# echo $?
0
#
```
-初始化完成,下面就是拷贝配置文件和启动脚本,配置文件在哪?在这个目录下support-files/
```
# ls support-files/
binary-configuremagicmy-default.cnfmysqld_multi.servermysql-log-rotatemysql.server
#
# ls support-files/my-default.cnf
support-files/my-default.cnf
```
-这里面大部分都是注释文件
```
# 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.
# 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.
"support-files/my-default.cnf" 31L, 1126C
```
-下面就是拷贝配置文件和启动脚本
```
# cp support-files/my-default.cnf /etc/my.cnf^C
```
- 拷贝之前 也可以看下系统自带的my.cnf 文件,要用自带的my.conf 需要修改里面配置文件
```
# ls /etc/my.cnf
/etc/my.cnf
# rpm -qf /etc/my.cnf
mariadb-libs-5.5.52-1.el7.x86_64
#
# vim /etc/my.cnf
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
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
```
- 需要修改/etc/my.cnf用默认的配置文件
```
datadir=/data/mysql
socket=/tmp/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
#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
~
:wq
```
- 再一个就是它的启动脚本
```
# ls
bin dataincludeman mysql-testscriptssql-bench
COPYINGdocslib my.cnfREADME share support-files
#
```
# 11.5 MySQL 安装 (下)
- 再一个就是它的启动脚本
```
# ls
bin dataincludeman mysql-testscriptssql-bench
COPYINGdocslib my.cnfREADME share support-files
#
# ls support-files/
binary-configuremy-default.cnf mysql-log-rotate
magic mysqld_multi.servermysql.server
```
- 把support-files/mysql.server脚本 拷贝到 改名 /etc/init.d/mysqld
```
# cp support-files/mysql.server /etc/init.d/mysqld
#
# vi /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=
-- INSERT --
```
- 改下basedir datadir
```
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.
basedir=/usr/local/mysql
datadir=/data/mysql
:wq
```
-看下权限是755
```
# vi /etc/init.d/mysqld
# ls -l /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10902 9月23 13:27 /etc/init.d/mysqld
#
```
- 如果想让它开机启动,需要把它加入到系统服务列表里去 下次开机会自动启动
```
# chkconfig --add mysqld
# chkconfig --list
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies '。
mysqld 0:关1:关2:开3:开4:开5:开6:关
netconsole 0:关1:关2:关3:关4:关5:关6:关
network 0:关1:关2:开3:关4:关5:关6:关
#
```
- 同样的,也可以直接用命令把它启动起来
/etc/init.d/mysql start
- 也可以这样service mysqld start
```
# ls -l /etc/init.d/mysqld
-rwxr-xr-x 1 root root 10875 9月23 18:13 /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
# vim /etc/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
Starting MySQL.Logging to '/data/mysql/aminglinux-001.err'.
.. SUCCESS!
#
```
- 查看下服务 是否有,看下进程
```
# ps aux |grep mysql
root 61100.00.1117641580 pts/0 S 18:15 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-001.pid
mysql 63200.8 45.6 973052 456580 pts/0 Sl 18:15 0:01 /usr/local/mysq/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-001.err --pid-file=/data/mysql/aminglinux-001.pid --socket=/tmp/mysql.sock --port=3306
root 63860.00.0 112664 976 pts/0 S+ 18:19 0:00 grep --color=auto mysql
# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1131/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1648/master
tcp6 0 0 :::3306 :::* LISTEN 6320/mysqld
tcp6 0 0 :::22 :::* LISTEN 1131/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1648/master
#
```
- 如果说有一天你没有办法把启动脚本放到/etc/init.d/下去,或者说你根本就没有这样的启动模板去拷贝,可以用这种方法去启动
-首先我们先给mysqld 停掉
```
# service mysqld stop
Shutting down MySQL.. SUCCESS!
# !ps
ps aux |grep mysql
root 64270.00.0 112664 972 pts/0 S+ 18:23 0:00 grep --color=auto mysql
```
- 使用这种方法命令行的方式启动
```
# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql &
6716
完成 /usr/local/mysql/bin/mysqld_safe --default-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
# 170923 18:28:43 mysqld_safe Logging to '/data/mysql/aminglinux-001.err'.
170923 18:28:43 mysqld_safe Starting mysqld daemon with databases from /data/mysql
#
# ps aux |grep mysql
root 67160.00.1 1132561584 pts/0 S 18:28 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
mysql 69140.5 45.4 973052 454888 pts/0 Sl 18:28 0:00 /usr/local/mysql/binmysqld --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/aminglinux-001.err --pid-file=/data/mysql/aminglinux-001.pid --socket=/tmp/mysql.sock --port=3306
root 69370.00.0 112664 976 pts/0 S+ 18:29 0:00 grep --color=auto mysql
#
# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1131/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1648/master
tcp6 0 0 :::3306 :::* LISTEN 6914/mysqld
tcp6 0 0 :::22 :::* LISTEN 1131/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1648/master
#
```
- 那怎么去关呢,可以用killall mysqld 命令 把这个服务停掉
```
# killall mysqld
# 170923 18:32:07 mysqld_safe mysqld from pid file /data/mysql/aminglinux-001.pid ended
# !ps
ps aux |grep mysql
root 69500.00.0 112664 976 pts/0 R+ 18:32 0:00 grep --color=auto mysq
+完成 /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf
#
```
-建议大家用killall 安全一些,先停止当前的写读操作,把那些没有完成磁盘写入的数据写到磁盘里去,知道写完之后 才把进程杀死,
-如果以后遇到 mysqld 的进程 适中杀不死,等了好久没有把进程杀死ps 还有进程,那说明你的数据量很大,正在慢慢的写入磁盘离去,不要强制用kill,很容易导致数据丢失,就慢慢的等就好了,
-mysql 有俩个引擎 一个是innodb一个是myisam(存储量比较小)
## 扩展
- mysql5.5源码编译安装 http://www.aminglinux.com/bbs/thread-1059-1-1.html
- MYSQL5.5源码安装 linux下,首先安装必要的库
```
yum -y install gcc*
###### 安装 MYSQL ######
首先安装camke
一、支持YUM,则
yum install -y cmake
二、也可以源码安装
cd /usr/local/src
#下载cmake
wget http://www.cmake.org/files/v2.8/cmake-2.8.7.tar.gz
tar zxvf cmake-2.8.7.tar.gz
cd cmake-2.8.7
#安装cmake
./configure
make
make install
安装 MYSQL
官网下载 MYSQL5.5版本 linux下源码包
http://dev.mysql.com/downloads/
安装
groupadd mysql
useradd -g mysql mysql
tar zxvf mysql-5.2.25.tar.gz
cd mysql-5.2.25
#cmake. //默认情况下安装,安装目录为/usr/local/mysql数据目录为/usr/local/mysql/data
#也可以指定参数安装,如指定UTF8,数据引擎等
#具体参照http://dev.mysql.com/doc/refman/ ... ration-options.html
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mysql/data -DDEFAULT_CHARSET=utf8-DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS:STRING=all -DWITH_DEBUG=0 -DWITH_SSL=yes -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1
make && make install
cd /usr/local/mysql
chown -R mysql:mysql/usr/local/mysql
./scripts/mysql_install_db --user=mysql-datadir=/mysql/data
#此处如不指定datadir,到启动时会报错
chown -R root .
chown -R mysql data
cp support-files/my-medium.cnf /etc/my.cnf
bin/mysqld_safe --user=mysql &
# Next command is optional
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
```
到此,安装完成
- mysql5.7二进制包安装(变化较大) http://www.apelearn.com/bbs/thread-10105-1-1.html
- mysql5.7 二进制包安装
```
1. 下载包
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
若该链接失效,请到r.aminglinux.com 找最新的下载地址。
2. 解压
tarxxvf mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz
mvmysql-5.7.12-linux-glibc2.5-x86_64/usr/local/mysql
3. 初始化
useradd -M -s /sbin/nologinmysql
mkdir -p /data/mysql
chown mysql /data/mysql
cd /usr/local/mysql
./bin/mysqld--initialize --user=mysql --datadir=/data/mysql
注意,这一步最后一行会有一个提示
A temporary password is generated for root@localhost: B*s1i(*,kXwg
最后面的字符串为root密码。
./bin/mysql_ssl_rsa_setup --datadir=/data/mysql
4. 拷贝配置文件和启动脚本
cp support-files/my-default.cnf/etc/my.cnf
vim /etc/my.cnf //编辑或者修改
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
socket = /tmp/mysql.sock
cp support-files/mysql.server /etc/init.d/mysqld
vi /etc/init.d/mysqld //编辑或者修改
basedir=/usr/local/mysql
datadir=/data/mysql
5. 启动服务
/etc/init.d/mysqld start
6. 设置root密码
使用初始化密码登录
/usr/local/mysql/bin/mysql -uroot -p'B*s1i(*,kXwg'//进入后直接设置密码
mysql>set password = password('mypass'); //一定要设置一下新密码
退出来,再使用新的密码登录就可以了
还有一种情况,就是不知道初始化密码
vi /etc/my.cnf
在下面增加一行
skip-grant-tables
重启/etc/init.d/mysqld restart
/usr/local/mysql/bin/mysql -uroot
mysql> update user set authentication_string=password('123333') where user='root';
退出来后,更改my.cnf,去掉刚加的 skip-grant-tables
重启 /etc/init.d/mysqld restart
```
- 此时就可以使用新的密码了。
页:
[1]