Mysql8 MHA高可用搭建之数据库二进制安装

环境准备:
机器配置:

mysql8单机安装
1、二进制安装:
2、编辑配置文件:
3、初始化数据库:
4、配置环境变量:
5、制作启动文件:
6、用初始密码进入mysql,修改初始密码:
7、一些问题及解决办法

环境准备:

操作系统:centos7.3
mysql版本:mysql 8.0.26
mha版本:0.58

机器配置

节点ip 角色 机器属性
192.168.100.161 主 +MHA node 虚拟机
192.168.100.162 主备+MHA node 虚拟机
192.168.100.163 从节点+MHA manager+MHA node 虚拟机
192.168.100.166 vip 虚拟IP

mysql8单机安装(三台虚拟机都需要安装)

1、二进制安装:

查看 之前是否安装过mariadb 或者 mysql

rpm -qa | grep mariadb
rpm -pa | grep mysql

如果有则卸载

rpm -e mariadb-libs-5.5.52-1.el7.x86_64 --nodeps 

再次检查

rpm -qa | grep mariadb
rpm -pa | grep mysql

下载地址 https://dev.mysql.com/downloads/mysql/
备用地址 http://mirrors.163.com/mysql/Downloads/MySQL-8.0/

mkdir -p /data1/mysql8
cd /data1/mysql8/

上传mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz到/data1/mysql8/下
或者下载:
wget http://mirrors.163.com/mysql/Downloads/MySQL-8.0/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
并解压

xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
tar -xvf  mysql-8.0.26-linux-glibc2.12-x86_64.tar
mv mysql-8.0.26-linux-glibc2.12-x86_64    /usr/local/mysql8

安装epel源

wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  /root
cd /root
yum install -y epel-release-latest-7.noarch.rpm
yum repolist

安装依赖

yum -y install perl  net-tools numactl.x86_64

创建用户和用户组

groupadd mysql
useradd -g mysql mysql

创建mysql的数据、日志等数据存储目录

# pwd
/data1/mysql8

# mkdir {data,log,var}
# ll /data1/mysql8
drwxr-xr-x 2 root  root           6 2月   7 16:30 data
drwxr-xr-x 2 root  root           6 2月   7 16:30 log
drwxr-xr-x 2 root  root           6 2月   7 16:30 var

授权

chown -R mysql:mysql  /data1/mysql8/
chown -R mysql:mysql  /usr/local/mysql8/

# ll /data1/mysql8
drwxr-xr-x 2 mysql mysql          6 2月   7 16:30 data
drwxr-xr-x 2 mysql mysql          6 2月   7 16:30 log
drwxr-xr-x 2 mysql mysql          6 2月   7 16:30 var

# ll /usr/local/mysql8

drwxr-xr-x  2 mysql mysql   4096 2月   8 16:31 bin
drwxr-xr-x  2 mysql mysql     55 7月   1 2021 docs
drwxr-xr-x  3 mysql mysql    282 7月   1 2021 include
drwxr-xr-x  6 mysql mysql    201 7月   1 2021 lib
-rw-r--r--  1 mysql mysql 276551 7月   1 2021 LICENSE
drwxr-xr-x  4 mysql mysql     30 7月   1 2021 man
-rw-r--r--  1 mysql mysql   1154 2月  12 19:48 my.cnf
-rw-r--r--  1 mysql mysql    666 7月   1 2021 README
drwxr-xr-x 28 mysql mysql   4096 7月   1 2021 share
drwxr-xr-x  2 mysql mysql     77 7月   1 2021 support-files

2、编辑配置文件:

  • 注意默认可能有/etc/my.cnf存在,备份 cp /etc/my.cnf /etc/my.cnf_back
  • 在设置mysql数据库新密码前先不要在配置文件中设置“skip-name-resolve” 否则会报“Host ‘127.0.0.1’ is not allowed to connect to this MySQL server”解决办法就是先暂时注释掉这一条,设置好密码后再删除注释符号。
# pwd
/usr/local/mysql8
# vim my.cnf

配置文件内容如下
[mysql]
default-character-set=utf8mb4
socket=/data1/mysql8/var/mysql.sock

[mysqld]
port=3306
socket=/data1/mysql8/var/mysql.sock
basedir=/usr/local/mysql8
datadir=/data1/mysql8/data
lower_case_table_names=1
server-id=161
log-bin=/data1/mysql8/data/mysql-bin
relay_log=relay_bin
log_slave_updates=on
pid-file=/data1/mysql8/var/mysqld.pid
gtid_mode=on
enforce_gtid_consistency=on
binlog_format=row

skip-name-resolve
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
character-set-client-handshake=FALSE
#symbolic-links=0  #mysql8默认禁用符号软连接
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'

max_connections=200
max_connect_errors=1000
max_allowed_packet=200M

default-storage-engine=INNODB
innodb_buffer_pool_size=200M

#explicit_defaults_for_timestamp=1
log-output=FILE
slow_query_log=ON
slow_query_log_file=/data1/mysql8/log/slow.log
long_query_time=5
log-error=/data1/mysql8/log/liandodb_error.log
default-authentication-plugin=mysql_native_password

[client]
port= 3306
default-character-set=utf8mb4
socket=/data1/mysql8/var/mysql.sock


3、初始化数据库:

  • 注意:--lower-case-table-names=1
    • MySQL8.0 新增了data dictionary的概念,数据初始化的时候在linux下默认使用lower-case-table-names=0的参数,数据库启动的时候读取的my.cnf文件中的值。若二者值不一致则在mysql的错误日志中记录报错信息。在MySQL 5.7之前则允许数据库初始化和启动的值不一致且以启动值为准。在MySQL 官方提供的RPM包中默认是使用lower-case-table-names=0,不太适合生产环境部署。在生产环境建议使用官方的二进制包。
    • 解决办法:
      在mysql数据库初始化的时候指定不区分大小写,在数据库实例启动的时候也要指定不区分大小写。即数据库初始化时lower_case_table_names的值和数据库启动时的值需要一样。
      在实际开发生产的应用中多是不区分大小写的即lower-case-table-names=1。
$ mysqld --initialize --lower-case-table-names=1 --user=mysql --basedir=/usr/local/mysql8 --datadir=/data1/mysql8/data
配置文件中添加如下设置
my.cnf
 [mysqld]
 lower_case_table_names=1
  • 数据库初始化
# chown -R mysql:mysql  /usr/local/mysql8/
# su mysql
$ mysqld --initialize --lower-case-table-names=1 --user=mysql --basedir=/usr/local/mysql8 --datadir=/data1/mysql8/data
2022-02-11T02:34:38.496424Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2022-02-11T02:34:38.496443Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
2022-02-11T02:34:38.497125Z 0 [System] [MY-013169] [Server] /usr/local/mysql8/bin/mysqld (mysqld 8.0.26) initializing of server in progress as process 4323
2022-02-11T02:34:38.512414Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2022-02-11T02:34:42.079092Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2022-02-11T02:34:44.651608Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2022-02-11T02:34:44.652395Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2022-02-11T02:34:44.739567Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ik5F!uJdei/s

# 执行完后生成mysql默认root用户的密码 root@localhost: ik5F!uJdei/s    之后登录需要用到这里记录下来。

4、配置环境变量:

vim /etc/profile
# MYSQL8_HOME
MYSQL8_HOME=/usr/local/mysql8
export PATH=$PATH:$MYSQL8_HOME/bin

#刷新使环境变量生效
source /etc/profile

5、制作启动文件:

vim /etc/systemd/system/mysql8d.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=/usr/local/mysql8/bin/mysqld --defaults-file=/usr/local/mysql8/my.cnf
LimitNOFILE = 65536
LimitNPROC = 65536

保存退出
重新加载service文件
systemctl daemon-reload
设置开机启动
systemctl enable mysql8d
日常启停命令
systemctl start mysql8d.service
systemctl status mysql8d.service
systemctl stop mysql8d.service

6、用初始密码进入mysql,修改初始密码:

mysql -u root -p -h 127.0.0.1

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'pass123';

mysql> flush privileges;

mysql> use mysql;

mysql> select host,user from user where user='root';

+-----------+------+
| host      | user |
+-----------+------+
| localhost | root |
+-----------+------+


mysql> update user set host='%',plugin='mysql_native_password' where user='root';

mysql> flush privileges;

mysql> select host,user from user where user='root';

+------+------+
| host | user |
+------+------+
| %    | root |
+------+------+

7、一些问题及解决办法

#问题1、密码失效
    1、修改my.cnf 在[mysqld]段落下增加 skip-grant-tables=1 #登录时,跳过权限验证
    2、update user set authentication_string=MD5('root') where user='root' and Host = 'localhost';
    或者update user set authentication_string=SHA1('root') where user='root' and Host = 'localhost';
    FLUSH PRIVILEGES;
    select host, user, authentication_string, plugin from user;

    如果直接执行ALTER USER报错,可以按下面顺序执行
    update user set host='%',plugin='mysql_native_password',authentication_string='' where user='root';
    FLUSH PRIVILEGES;
    ALTER USER 'root'@'localhost'  IDENTIFIED WITH mysql_native_password BY '123456';

#问题2、 Starting MySQL.2021-02-05T01:43:59.542284Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists.
    解决:mkdir /var/lib/mysql
    
#问题3、Starting MySQL... ERROR! The server quit without updating PID file (/data1/mysql8/data/CT-DevOps-DB.pid).
    解决查看错误日志:Could not create unix socket lock file /var/lib/mysql/mysql.sock.lock.
    这个是权限不足导致,设置权限 chown -R mysql:mysql /var/lib/mysql/
    grant all privileges on *.* to 'root'@'%' ;
    
# 问题4、navicate连接不上
    解决:添加需要监听的端口/sbin/iptables -I INPUT -p tcp --dport 3307 -j ACCEPT
    iptables-save > /etc/sysconfig/iptables iptables-save是将规则追加到一个文件

文章参考:
原文链接:https://blog.csdn.net/qq_15350581/article/details/114090794

你可能感兴趣的:(Mysql8 MHA高可用搭建之数据库二进制安装)