[root@db01 ~]# cat /etc/my.cnf
[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3306/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
[标签项] : 用来表示不同的程序
服务端: [mysqld] [mysqld_safe] [server]
客户端: [mysql] [mysqldump] [client]
配置=xxx : 设置的参数键值对
user=mysql # 数据库管理用户
basedir=/data/app/mysql # 程序路径
datadir=/data/3306/data # 数据路径
socket=/tmp/mysql.sock # socket文件位置
# 默认配置文件读取顺序
[root@db01 ~]# mysqld --help --verbose |grep my.cnf
/etc/my.cnf ---》 /etc/mysql/my.cnf ---》 /usr/local/mysql/etc/my.cnf ----》 ~/.my.cnf
建议: 每个数据库保留一个配置文件。
彩蛋:
如果有多个配置文件,例如
/etc/my.cnf ---> port=3306
~/.my.cnf ---> port=3308
数据库启动时,port是多少?
# 手工定义配置文件读取路径
mysqld --defaults-file=/opt/my1.cnf &
mysqld_safe --defaults-file=/opt/my2.cnf &
#1. 停数据库
[root@db01 tmp]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
[root@db01 tmp]#
#2. 启动数据库到“安全”模式
mysqld_safe --skip-grant-tables --skip-networking &
参数作用:
--skip-grant-tables : 跳过授权表,不开启验证功能。
--skip-networking : 阻止所有TCP/IP网络连接。
#3. 改密码
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123456';
#4. 重启至正常模式
[root@db01 tmp]# /etc/init.d/mysqld restart
[root@db01 ~]# mkdir -p /data/330{7..9}/data /data/330{7..9}/logs
cat >/data/3307/my.cnf <[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3307/data
server_id=7
port=3307
log_bin=/data/3307/logs/mysql-bin
socket=/tmp/mysql3307.sock
EOF
cat >/data/3308/my.cnf <[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3308/data
server_id=8
port=3308
log_bin=/data/3308/logs/mysql-bin
socket=/tmp/mysql3308.sock
EOF
cat >/data/3309/my.cnf <[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3309/data
server_id=9
port=3309
log_bin=/data/3309/logs/mysql-bin
socket=/tmp/mysql3309.sock
EOF
[root@db01 ~]# chown -R mysql. /data/*
mv /etc/my.cnf /etc/my.cnf.bak
mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3307/data
mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3308/data
mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql --datadir=/data/3309/data
mv /etc/my.cnf.bak /etc/my.cnf
[root@db01 ~]# mysqld_safe --defaults-file=/data/3307/my.cnf &
[root@db01 ~]# mysqld_safe --defaults-file=/data/3308/my.cnf &
[root@db01 ~]# mysqld_safe --defaults-file=/data/3309/my.cnf &
cat > /etc/systemd/system/mysqld3307.service <[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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=/data/app/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf
LimitNOFILE = 5000
EOF
cat > /etc/systemd/system/mysqld3308.service <[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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=/data/app/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf
LimitNOFILE = 5000
EOF
cat > /etc/systemd/system/mysqld3309.service <[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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=/data/app/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf
LimitNOFILE = 5000
EOF
[root@db01 ~]# systemctl start mysqld3307
[root@db01 ~]# systemctl start mysqld3308
[root@db01 ~]# systemctl start mysqld3309
mysql -S /tmp/mysql3307.sock
grant all on *.* to root@'10.0.0.%' identified by '123';
mysql -S /tmp/mysql3308.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123';"
mysql -S /tmp/mysql3309.sock -e "grant all on *.* to root@'10.0.0.%' identified by '123';"
tar xf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
tar xf mysql-8.0.18-linux-glibc2.12-x86_64.tar.xz
ln -s mysql-8.0.18-linux-glibc2.12-x86_64 mysql80
ln -s mysql-5.6.46-linux-glibc2.12-x86_64 mysql56
mkdir -p /data/331{7..8}/data /data/331{7..8}/logs
cat >/data/3317/my.cnf <[mysqld]
user=mysql
basedir=/data/app/mysql56
datadir=/data/3317/data
server_id=17
port=3317
log_bin=/data/3317/logs/mysql-bin
socket=/tmp/mysql3317.sock
EOF
cat >/data/3318/my.cnf <[mysqld]
user=mysql
basedir=/data/app/mysql80
datadir=/data/3318/data
server_id=18
port=3318
log_bin=/data/3318/logs/mysql-bin
socket=/tmp/mysql3318.sock
EOF
[root@db01 ~]# chown -R mysql. /data/*
mv /etc/my.cnf /etc/my.cnf.bak
vim /etc/profile
#注释环境变量:
# export PATH=/data/app/mysql/bin:$PATH
# 最好重连一个Xshell窗口
[root@db01 ~]# mysql -V
-bash: mysql: command not found
[root@db01 ~]#
# 5.6初始化
/data/app/mysql56/scripts/mysql_install_db --user=mysql --basedir=/data/app/mysql56 --datadir=/data/3317/data
# 8.0初始化
/data/app/mysql80/bin/mysqld --initialize-insecure --user=mysql --basedir=/data/app/mysql80 --datadir=/data/3318/data
cat > /etc/systemd/system/mysqld3317.service <[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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=/data/app/mysql56/bin/mysqld --defaults-file=/data/3317/my.cnf
LimitNOFILE = 5000
EOF
cat > /etc/systemd/system/mysqld3318.service <[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://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=/data/app/mysql80/bin/mysqld --defaults-file=/data/3318/my.cnf
LimitNOFILE = 5000
EOF
[root@db01 app]# /data/app/mysql56/bin/mysql -S /tmp/mysql3317.sock
[root@db01 app]# /data/app/mysql80/bin/mysql -S /tmp/mysql3318.sock
a. 安装 新版本软件
略。
b. 停原库 (5.6.46)
# 1. 快速关库功能关闭(优雅关闭、干净的关闭)
vim /data/3317/my.cnf
## 添加以下配置
innodb_fast_shutdown=0
[root@db01 data]# systemctl stop mysqld3317
c. 使用高版本软件挂低版本数据启动
[root@db01 data]# vim /data/3317/my.cnf
[mysqld]
user=mysql
basedir=/data/app/mysql
datadir=/data/3317/data
socket=/tmp/mysql3317.sock
port=3317
server_id=17
innodb_fast_shutdown=0
[root@db01 data]# /data/app/mysql/bin/mysqld_safe --defaults-file=/data/3317/my.cnf --skip-grant-tables --skip-networking &
d. 升级 (升级到8.0可以省略)
[root@db01 data]# /data/app/mysql/bin/mysql_upgrade -S /tmp/mysql3317.sock --force
e. 重启数据库到正常状态
[root@db01 data]# /data/app/mysql/bin/mysqladmin -S /tmp/mysql3317.sock shutdown
[root@db01 data]# vim /etc/systemd/system/mysqld3317.service
# 修改以下内容
ExecStart=/data/app/mysql/bin/mysqld --defaults-file=/data/3317/my.cnf
# 启动数据库
[root@db01 data]# systemctl start mysqld3317
# 连接查看
[root@db01 data]# /data/app/mysql/bin/mysql -S /tmp/mysql3317.sock
a. 下载 8.0.18 版本的 mysql-shell,并安装 。
https://downloads.mysql.com/archives/
[root@db01 app]# yum install -y mysql-shell-8.0.18-1.el7.x86_64.rpm
b. 创建连接用户
[root@db01 data]# /data/app/mysql/bin/mysql -S /tmp/mysql3317.sock
mysql> grant all on *.* to root@'10.0.0.%' identified by '123';
mysql> drop user root@'127.0.0.1';
mysql> drop user root@'db01';
mysql> drop user ''@'db01';
mysql> drop user ''@'localhost';
mysql> drop user root@'::1';
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | 10.0.0.% |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
c. 预 检查
[root@db01 data]# mysqlsh root:[email protected]:3317 -e "util.checkForServerUpgrade()" >/tmp/up.log
d. 停原库(5.7.28)
vim /data/3317/my.cnf
## 添加以下配置
innodb_fast_shutdown=0
[root@db01 data]# systemctl stop mysqld3317
e. 使用高版本软件挂低版本数据启动
[root@db01 data]# vim /data/3317/my.cnf
[mysqld]
user=mysql
basedir=/data/app/mysql80
datadir=/data/3317/data
socket=/tmp/mysql3317.sock
port=3317
server_id=17
innodb_fast_shutdown=0
[root@db01 data]# /data/app/mysql80/bin/mysqld_safe --defaults-file=/data/3317/my.cnf --skip-grant-tables --skip-networking &
f. 重启数据库到正常状态
[root@db01 data]# /data/app/mysql80/bin/mysqladmin -S /tmp/mysql3317.sock shutdown
[root@db01 data]# vim /etc/systemd/system/mysqld3317.service
# 修改以下内容
ExecStart=/data/app/mysql80/bin/mysqld --defaults-file=/data/3317/my.cnf
# 启动数据库
[root@db01 data]# systemctl start mysqld3317
# 连接查看
[root@db01 data]# /data/app/mysql80/bin/mysql -S /tmp/mysql3317.sock
5.7.28 —》 5.7.10 inplace downgrade演练
原版本:
软件: 5.7.28 /data/app/mysql + 数据:/data/3306/data
目标版本: 5.7.10 /data/app/mysql5710
#安装 5.7.10 (低) 二进制版本
[root@db01 app]# ln -s mysql-5.7.10-linux-glibc2.5-x86_64 mysql5710
#针对5728版本(高)进行处理工作
https://dev.mysql.com/doc/refman/5.7/en/downgrading-to-previous-series.html
[root@db01 app]# cp /etc/my.cnf.bak /etc/my.cnf
[root@db01 app]# /etc/init.d/mysqld restart
[root@db01 app]# /data/app/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock
set sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ;
set global sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' ;
select @@sql_mode;
ALTER TABLE mysql.proc MODIFY definer char(77) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '';
ALTER TABLE mysql.event MODIFY definer char(77) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL DEFAULT '';
ALTER TABLE mysql.tables_priv MODIFY Grantor char(77) COLLATE utf8_bin NOT NULL DEFAULT '';
ALTER TABLE mysql.procs_priv MODIFY Grantor char(77) COLLATE utf8_bin NOT NULL DEFAULT '';
#优雅的关闭5.7.28(高)。
[root@db01 app]# /data/app/mysql/bin/mysql -uroot -p123456 -S /tmp/mysql.sock
set global innodb_fast_shutdown=0 ;
[root@db01 app]# /data/app/mysql/bin/mysqladmin -uroot -p123456 shutdown
#删除ib_logfile*
[root@db01 mysql5710]# rm -rf /data/3306/data/ib_logfile*
#替换配置文件(替换成低版本)
[root@db01 mysql5710]# vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/data/app/mysql5710
#basedir=/data/app/mysql
datadir=/data/3306/data
socket=/tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
#低版本启动高版本数据库
/data/app/mysql5710/bin/mysqld --skip-grant-tables --skip-networking &
#执行upgrade
[root@db01 ~]# /data/app/mysql5710/bin/mysql_upgrade -uroot -p123456 --force
#启动到正常模式
[root@db01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
/data/app/mysql5710/bin/mysql -uroot -p123456 -S /tmp/mysql.sock