一、mysql启动关闭
1.mysql启动流程
/etc/init.d/mysqld start ------> mysql.server ------> mysqld_safe ------> mysqld
systemctl start mysql ------> mysqld_safe ------> mysqld
mysqld_safe --defaults-file=/etc/my.cnf ------> mysqld_safe ------> mysqld
2.关闭数据库
/etc/init.d/mysqld stop
systemctl stop mysql
mysqladmin -uroot -p123 shutdown
#不建议使用的关闭方法
kill -9 mysqlpid
killall mysqld
pkill mysqld
二、mysql二进制指定目录安装数据库
1.下载源码包
[root@db02 ~]# wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
2.安装依赖
[root@db02 ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ glibc cmake autoconf
3.解压
[root@db02 ~]# tar xf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
4.创建自定义目录
[root@db02 ~]# mkdir /server
5.移动数据
[root@db02 ~]# mv mysql-5.6.46-linux-glibc2.12-x86_64 /server/mysql-5.6.46
[root@db02 ~]# ln -s /server/mysql-5.6.46 /server/mysql
6.创建用户
[root@db02 ~]# useradd -M -s /sbin/nologin mysql
7.拷贝配置文件和脚本
[root@db02 ~]# cd /server/mysql/support-files/
[root@db02 support-files]# cp mysql.server /etc/init.d/mysqld
[root@db02 support-files]# cp my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
8.初始化数据库
[root@db02 support-files]# cd /server/mysql/scripts/
[root@db02 scripts]# ll
total 36
-rwxr-xr-x 1 7161 31415 34977 Sep 27 2019 mysql_install_db
[root@db02 scripts]# ./mysql_install_db --user=mysql --basedir=/server/mysql --datadir=/server/mysql/data
9.启动数据库
[root@db02 scripts]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 244: my_print_defaults: command not found
/etc/init.d/mysqld: line 264: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
#启动报错,因为数据库在cmake阶段指定的目录都是 /usr/local,所以启动文件与我们指定的目录不一致
#修改启动文件
[root@db02 scripts]# sed -i 's#/usr/local#/server#g' /server/mysql/bin/mysqld_safe /etc/init.d/mysqld
#再次启动数据
[root@db02 scripts]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/server/mysql/data/db02.err'.
SUCCESS!
10.配置system管理mysql
#1.system配置
[root@db02 ~]# vim /usr/lib/systemd/system/mysql.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/server/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#2.重新加载启动列表
[root@db02 ~]# systemctl daemon-reload
11.system启动失败
#因为除了我们修改的启动文件,还有很多文件指定了 /usr/local
[root@db02 ~]# grep -r '/usr/local' /server/mysql/*
12.配置mysql的配置文件
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
basedir=/server/mysql
datadir=/server/mysql/data
[root@db02 ~]# systemctl start mysql
[root@db02 ~]# !ps
ps -ef | grep mysql
mysql 7988 1 18 18:39 ? 00:00:00 /server/mysql/bin/mysqld --defaults-file=/etc/my.cnf
root 8011 7404 0 18:39 pts/0 00:00:00 grep --color=auto mysql
13.配置环境变量
[root@db02 ~]# vim /etc/profile.d/mysql.sh
export PATH=/server/mysql/bin:$PATH
[root@db02 ~]# source /etc/profile
三、mysql配置
1.配置mysql的地方
1.cmake阶段:cmake编译安装时指定目录到程序
2.配置文件:/etc/my.cnf ... 等
3.命令行指定配置
--skip-grant-tables
--skip-networking
--datadir=/application/mysql/data
--basedir=/application/mysql
--defaults-file=/etc/my,cnf
--pid-file=/application/mysql/data/db01.pid
--socket=/application/mysql/data/mysql.sock
--user=mysql
--port=3306
--log-error=/application/mysql/data/db01.err
2.配置文件
1)配置文件读取顺序
/etc/my.cnf
/etc/mysql/my.cnf
$MYSQL_HOME/my.cnf(前提是在环境变量中定义了MYSQL_HOME变量)
defaults-extra-file (类似include)
~/.my.cnf
2)配置文件生效顺序
~/.my.cnf
defaults-extra-file (类似include)
$MYSQL_HOME/my.cnf(前提是在环境变量中定义了MYSQL_HOME变量)
/etc/mysql/my.cnf
/etc/my.cnf
3)配置文件生效顺序测试
#配置~/.my.cnf
[root@db02 ~]# vim ~/.my.cnf
[mysqld]
server_id=1
#配置$basedir/my.cnf
[root@db02 ~]# cat /server/mysql/my.cnf
[mysqld]
server_id=2
#配置/etc/mysql/my.cnf
[root@db02 ~]# cat /etc/mysql/my.cnf
[mysqld]
server_id=3
#配置/etc/my.cnf
[root@db02 ~]# cat /etc/my.cnf
[mysqld]
server_id=4
#重启数据库(不要使用system,因为里面指定/etc/my.cnf)
[root@db02 ~]# systemctl stop mysql.service
[root@db02 ~]# /etc/init.d/mysqld start
#验证
mysql> show variables like '%server_id%';
3.配置的生效顺序
#cmake
socket=/tmp/mysql.sock
#配置文件指定socket
[root@db02 ~]# vim /etc/my.cnf
[mysqld]
socket=/server/mysql/mysql.sock
#命令行配置
--socket=/opt/mysql.sock
#命令行指定文件
[root@db02 ~]# vim ~/1.txt
[mysqld]
socket=/mnt/mysql.sock
#测试优先级命令
[root@db02 ~]# mysqld_safe --defaults-file=~/1.txt --socket=/opt/mysql.sock
mysqld_safe 读取的是/etc/my.cnf
--defaults-file=~/1.txt 读取的是~/1.txt
--socket 指定的是 /opt/mysql.sock
4.优先级结论
#优先接结论:
1.命令行
2.defaults-file
3.配置文件
4.cmake
#最终总的配置优先级顺序:
命令行 >> defaults-file >> ~/.my.cnf >> $basedir/my.cnf >> /etc/mysql/my.cnf >> /etc/my.cnf >> cmake