安装MySQL
MySQL的几个常用安装包:rpm、源码、二进制免编译
cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-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
实验流程
[root@lyon-01 ~]# cd /usr/local/src/ //建议安装的部署程序放在这个目录下
[root@lyon-01 src]# uname -a //可以查看电脑是32位还是64位
Linux lyon-01 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
[root@lyon-01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz //下载64位的5.6版本的包。比较大,可能需要等会
[root@lyon-01 src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz //解压
[root@lyon-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
//将MySQL的解压目录移动到/usr/local/下,并改名字为mysql
[root@lyon-01 local]# cd mysql/ //务必确保在此之前么有mysql目录
[root@lyon-01 mysql]# ls
bin data include man README share support-files
COPYING docs lib mysql-test scripts sql-bench
[root@lyon-01 mysql]# useradd mysql 增加用户mysql
[root@lyon-01 mysql]# mkdir /data/ 创建存放数据的目录
[root@lyon-01 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 这里报了错,可以搜索,也可以yum list 模糊搜索
[root@lyon-01 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@lyon-01 mysql]# yum install -y perl-Data-Dumper.x86_64 //这个包
[root@lyon-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
echo $?
[root@lyon-01 mysql]# cp support-files/my-default.cnf /etc/my.cnf
系统本身自带一个my.cnf的文件 我们改成如下即可:
[mysqld]
datadir=/data/mysql //只修改这里
socket=/tmp/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 //注释
保存退出即可。
启动脚本部分
[root@lyon-01 mysql]# ls support-files/
binary-configure magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server
[root@lyon-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@lyon-01 mysql]# vi /etc/init.d/mysqld
修改这个地方即可
basedir=/usr/local/mysql
datadir=/data/mysql
保存退出
配置开机启动
[root@lyon-01 mysql]# chkconfig --add mysqld
[root@lyon-01 mysql]# chkconfig --list
注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。
如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
欲查看对特定 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关 服务开启
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
服务启动 :
[root@lyon-01 mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/lyon-01.err'.
.. SUCCESS! //出现success说明服务成功启动
[root@lyon-01 mysql]# ps aux |grep mysql
root 4190 0.0 0.1 11760 1584 pts/1 S 15:09 0:00 /bin/sh /usr/local/mysql/bin/mysqd_safe --datadir=/data/mysql --pid-file=/data/mysql/lyon-01.pid
mysql 4325 10.0 44.6 973044 450676 pts/1 Sl 15:09 0:02 /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/lyon-01.err --pid-file=/data/mysql/lyon-01.pid --socket=/tmp/mysql.sock
root 4353 0.0 0.0 112664 976 pts/1 S+ 15:09 0:00 grep --color=auto mysql
[root@lyon-01 mysql]# netstat -lnpt
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 1384/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1806/master
tcp6 0 0 :::3306 :::* LISTEN 4325/mysqld
tcp6 0 0 :::22 :::* LISTEN 1384/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1806/master
假设我们没有启动脚本 我们用下面这个方法启动
[root@lyon-01 mysql]# service mysqld stop //停止服务
Shutting down MySQL.. SUCCESS!
[root@lyon-01 mysql]# ps aux |grep mysqld
root 4392 0.0 0.0 112664 972 pts/1 S+ 15:17 0:00 grep --color=auto mysqld //已经停掉了
[root@lyon-01 mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql & //用这个命令,并放在后台执行
[1] 4401
[root@lyon-01 mysql]# 170926 15:19:54 mysqld_safe Logging to '/data/mysql/lyon-01.err'.
170926 15:19:55 mysqld_safe Starting mysqld daemon with databases from /data/mysql
[root@lyon-01 mysql]# !ps
ps aux |grep mysqld //看下是否已经启动
root 4401 0.0 0.1 113256 1584 pts/1 S 15:19 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql
mysql 4524 1.4 44.5 973044 449500 pts/1 Sl 15:19 0:01 /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/lyon-01.err --pid-file=/data/mysql/lyon-01.pid --socket=/tmp/mysql.sock
root 4552 0.0 0.0 112664 976 pts/1 R+ 15:21 0:00 grep --color=auto mysqld
[root@lyon-01 mysql]# !net //看一下mysql的端口
netstat -lnpt
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 1384/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1806/master
tcp6 0 0 :::3306 :::* LISTEN 4524/mysqld
tcp6 0 0 :::22 :::* LISTEN 1384/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1806/master
如何关闭呢
[root@lyon-01 support-files]# /etc/init.d/mysqld stop
或者 killall mysqld
尽量不要 kill pid的方法实现,但是建议用killall 更安全
数据库的读写很快,如果你kill 掉的话,内存里的数据可能会丢失,但是killall就不会,它会慢慢写入磁盘再停止。数据量较大的话时间可能会久点,但是安全。