Mysql部署

Posted 还行少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql部署相关的知识,希望对你有一定的参考价值。

一、环境

主机名IP地址操作系统mysql版本
mysql1192.168.10.180Centos 7Mysql5.7
mysql2192.168.10.181Centos 7Mysql5.7

二、单机部署

1、下载mysql安装包

下载地址:https://downloads.mysql.com/archives/community/

[root@mysql1 opt]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
--2022-03-21 14:13:51--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.197.180.248
Connecting to cdn.mysql.com (cdn.mysql.com)|23.197.180.248|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 666559924 (636M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz’

100%[========================================================================================================================>] 666,559,924 2.52MB/s   in 3m 55s 

2022-03-21 14:17:47 (2.71 MB/s) - ‘mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz’ saved [666559924/666559924]

2、创建用户

[root@mysql1 opt]# groupadd mysql
[root@mysql1 opt]# useradd mysql -g mysql -M -s /sbin/nologin

3、解压、重命名

[root@mysql1 opt]# tar xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz 
[root@mysql1 opt]# mv mysql-5.7.36-linux-glibc2.12-x86_64 /data/mysql

4、创建data目录,赋权

[root@mysql1 opt]# mkdir -p /data/mysql/data
[root@mysql1 opt]# chown -R mysql:mysql /data/mysql

5.配置my.cnf

[root@mysql1 opt]# cat /etc/my.cnf
[mysqld]
expire_logs_days = 20 
basedir=/data/mysql
datadir=/data/mysql/data
port = 3306
socket=/tmp/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=1
max_connections=5000

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

6、创建sock、pid、log文件

[root@mysql1 opt]# touch /tmp/mysql.sock
[root@mysql1 opt]# chown mysql:mysql /tmp/mysql.sock 
[root@mysql1 opt]# chmod 755 /tmp/mysql.sock 
[root@mysql1 opt]# mkdir /tmp/mysqld
[root@mysql1 opt]# touch /tmp/mysqld/mysqld.pid
[root@mysql1 opt]# chown -R mysql:mysql /tmp/mysqld
[root@mysql1 opt]# chown 755 /tmp/mysqld/mysqld.pid 
[root@mysql1 opt]# touch /var/log/mysqld.log
[root@mysql1 opt]# chown -R mysql:mysql /var/log/mysqld.log
[root@mysql1 opt]# chmod 755 /var/log/mysqld.log

7、初始化mysql、安全启动

[root@mysql1 bin]# ./mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
[root@mysql1 bin]# ./mysqld_safe --user=mysql &

8、修改密码(首次启动,密码在日志里)

[root@mysql1 bin]# grep "localhost" /var/log/mysqld.log 
2022-03-21T06:38:01.464837Z 1 [Note] A temporary password is generated for root@localhost: FY*<p(Du>5da
[root@mysql1 bin]# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.7.36-log

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> set password=password("123456");
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

9、优化管理方式

9.1 命令加入环境变量

[root@mysql1 bin]# tail -n 5 /etc/profile

unset i
unset -f pathmunge
export MYSQL_HOME=/data/mysql
export PATH="$PATH:$MYSQL_HOME/bin"
[root@mysql1 bin]# source /etc/profile

9.2 加入系统管理

[root@mysql1 bin]# cp -a /data/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql1 bin]# chkconfig --add /etc/init.d/mysqld
[root@mysql1 bin]# chkconfig mysqld on

9.3 重新启动mysql

[root@mysql1 bin]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      30640/mysqld        
[root@mysql1 bin]# pkill -9 mysqld
[1]+  Killed                  ./mysqld_safe --user=mysql
[root@mysql1 bin]# netstat -antp | grep mysqld
[root@mysql1 bin]# systemctl start mysqld
[root@mysql1 bin]# netstat -antp | grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      31276/mysqld        
[root@mysql1 bin]# 

三、主从搭建

1、修改mysql1的my.cnf

[root@mysql1 opt]# cat /etc/my.cnf
[mysqld]
expire_logs_days = 20 
basedir=/data/mysql
datadir=/data/mysql/data
port = 3306
socket=/tmp/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=1
max_connections=5000
log-slave-updates=true

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

2、部署mysql2

过程一样,修改mysql2的my.cnf

[root@mysql2 opt]# cat /etc/my.cnf
[mysqld]
expire_logs_days = 20 
basedir=/data/mysql
datadir=/data/mysql/data
port = 3306
socket=/tmp/mysql.sock

symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld/mysqld.pid
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
log-bin=mysql-bin
binlog-format=ROW
server_id=2
max_connections=5000
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

3、 主数据库对从库授权

mysql> grant replication slave on *.* to 'myslave'@'192.168.10.181' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      750 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

4、从库开启slave功能

mysql> change master to master_host='192.168.10.180',master_user='myslave',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=750;
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

5、查看slave状态

6、测试


四、mysql小版本升级

1、下载新版本mysql

[root@mysql1 opt]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
--2022-03-21 16:05:25--  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.44.105.4
Connecting to cdn.mysql.com (cdn.mysql.com)|23.44.105.4|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 666603757 (636M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’

100%[=========================================================>] 666,603,757 1.91MB/s   in 4m 21s 

2022-03-21 16:09:48 (2.43 MB/s) - ‘mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz’ saved [666603757/666603757]

2、备份现在的mysql

[root@mysql1 opt]# cd /data/
[root@mysql1 data]# cp -a mysql/ mysqlbak

3、解压新的安装包,覆盖老的目录

[root@mysql1 opt]# systemctl stop mysqld
[root@mysql1 opt]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz 
[root@mysql1 opt]# \\cp -frp mysql-5.7.37-linux-glibc2.12-x86_64/* /data/mysql

4、重启服务,升级

[root@mysql1 opt]# systemctl start mysqld
[root@mysql1 opt]# mysql_upgrade -uroot -p123456

5、重启mysql,查看数据

6、从库升级

[root@mysql2 opt]# systemctl stop mysqld
[root@mysql2 opt]# tar xf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
[root@mysql2 data]# cp -a mysql mysqlbak
[root@mysql2 opt]# \\cp -frp mysql-5.7.37-linux-glibc2.12-x86_64/* /data/mysq
[root@mysql2 opt]# systemctl start mysqld
[root@mysql2 opt]# mysql_upgrade -uroot -p123456

7、测试




以上是关于Mysql部署的主要内容,如果未能解决你的问题,请参考以下文章

网络云盘项目——Redis部署MySQL部署MySQL表设计

MySQLUbuntu 安装部署 MySQL

MySQLUbuntu 安装部署 MySQL

MySQLUbuntu 安装部署 MySQL

Docker应用部署

4步搞定MySQL安装部署(附MySQL一键式部署脚本)