直接在机器上部署
安装遇到问题
Downloading packages:Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
The GPG keys listed for the “MySQL 5.7 Community Server” repository are already installed but they are not correct for this package.
原因是Mysql的GPG升级了,需要重新获取
使用以下命令即可
rpm —import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
下载rpm、安装、启动
#由于CentOS7的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。#下载命令:cd /usr/localwget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm --no-check-certificate#然后进行yum源的安装:rpm -ivh mysql57-community-release-el7-9.noarch.rpmrpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022#安装完成后,就可以使用yum命令安装mysql了:yum -y install mysql-server#启动mysql:systemctl restart mysqld#查看mysql状态:systemctl status mysqld
配置配置文件my.cnf
vim /etc/my.cnf
[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-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.logpid-file=/var/run/mariadb/mariadb.pid## include all files from the config directory#!includedir /etc/my.cnf.d[mysqld]user=mysqlserver-id = 1port = 3306datadir=/var/lib/mysql/mysql_datatmpdir = /tmpskip-external-lockingskip_name_resolve = 1lower_case_table_names = 1max_connections = 8000max_connect_errors = 8000explicit_defaults_for_timestamp = 1max_allowed_packet = 256Minteractive_timeout = 1800wait_timeout = 1800tmp_table_size = 128Mmax_heap_table_size = 128Mquery_cache_size = 0query_cache_type = 0read_buffer_size = 1Mread_rnd_buffer_size = 2Mbinlog_cache_size = 8Mjoin_buffer_size = 128Mback_log = 600explicit_defaults_for_timestamp = falsequery_cache_wlock_invalidate = OFFsql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION#lc-messages-dir=/usr/share/mysql/english#######log set#############log_error = /var/lib/mysql/log/mysql.logslow_query_log = 1slow_query_log_file = /var/lib/mysql/log/slow.loglog_queries_not_using_indexes = 1long_query_time = 2expire_logs_days = 7log-bin=/var/lib/mysql/log/mysql-bin[client]default-character-set=utf8[mysql]default-character-set=utf8
配置密码并验证
获取mysql的临时密码:
grep ‘temporary password’ /var/log/mysqld.log
登录mysql:(密码为上一步骤获取的临时密码)
mysql -u root -p(此处不用输入密码,按下回车后会专门要你再输入密码的)
登录成功后,做任何操作都会被要求先修改密码
show databases;
请注意:如果修改的密码太过简单,依然会提示error,修改失败
因为5.7及以上版本的数据库对密码做了强度要求,默认密码的要求必须是大小写字母数字特殊字母的组合且至少要8位长度
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘新的密码’;
按照系统的默认要求,修改密码:
ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘-520Lemon-‘;
alter user ‘root’@’localhost’ identified by ‘Pass123456$’;
修改完成后,即可愉快的操作数据库:
show databases;
mysql集群(待补充)
下载
下载页面:https://github.com/mysql/mysql-server/releases/tag/mysql-5.7.37
https://dev.mysql.com/downloads/
https://downloads.mysql.com/archives/cluster/
下载链接:
https://downloads.mysql.com/archives/get/p/14/file/mysql-cluster-gpl-7.5.24-el7-x86_64.tar.gz
编写脚本mysql-install.sh
vim /usr/local/mysql-install.sh
chmod 755 /usr/local/mysql-install.sh
#!/bin/bashecho "############################# MySQL installing..........########################"echo "-----config my.cnf-----"mv /etc/my.cnf /etc/my.cnf_date +%Y%m%dSERVER_ID=ifconfig eth0 |grep inet |awk '{print $2}'|awk -F. '{print $3$4}'Mem=free -m |grep Mem |awk '{print $2}'SIZE=$[Mem*1/2]mysqldir=pwdcat >>/etc/my$ipaddr.cnf<<EOF[mysqld]user=mysqlserver-id = 1port = 3306basedir=${mysqldir}/mysqldatadir=${mysqldir}/mysql/mysql_datatmpdir = /tmpskip-external-lockingskip_name_resolve = 1lower_case_table_names = 1max_connections = 8000max_connect_errors = 8000explicit_defaults_for_timestamp = 1max_allowed_packet = 256Minteractive_timeout = 1800wait_timeout = 1800tmp_table_size = 128Mmax_heap_table_size = 128Mquery_cache_size = 0query_cache_type = 0read_buffer_size = 1Mread_rnd_buffer_size = 2Mbinlog_cache_size = 8Mjoin_buffer_size = 128Mback_log = 600explicit_defaults_for_timestamp = falsequery_cache_wlock_invalidate = OFFsql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION#######log set#############log_error = ${mysqldir}/mysql/log/mysql.logslow_query_log = 1slow_query_log_file = ${mysqldir}/mysql/log/slow.loglog_queries_not_using_indexes = 1long_query_time = 2expire_logs_days = 7log-bin=${mysqldir}/mysql/log/mysql-bin[client]default-character-set=utf8[mysql]default-character-set=utf8EOFecho "-----create group and user:mysql-----"groupadd mysqluseradd -r -g mysql -s /bin/false mysqlecho "----- install dependent package -----"yum -y install numactl libaio lrzsz wget#echo "-----Download MySQL-----"#if [ -d /soft ];thenecho "software dir is exist! Please check!"#elsemkdir -p /soft#fiwget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tarecho "-----Unzip MySQL-----"tar -zxvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tarmv mysql-5.7.28-1.el7.x86_64.rpm-bundle mysqlecho "-----create datadir and change owner of dir-----"mkdir -p mysql/mysql_datamkdir -p mysql/logchown -R mysql.mysql /etc/my.cnfchown -R mysql.mysql ${mysqldir}/mysql/chown -R mysql.mysql ${mysqldir}/mysql/mysql_data/echo "-----config profile-----"echo 'PATH=${mysqldir}/mysql/bin/:$PATH' >> /etc/profilesource /etc/profileecho "-----initialize MySQL-----"${mysqldir}/mysql/bin/mysqld --initialize --user=mysql --basedir=${mysqldir}/mysql --datadir=${mysqldir}/mysql/mysql_data/echo "-----start MySQL-----"cp ${mysqldir}/mysql/support-files/mysql.server /etc/init.d/mysqldln -s ${mysqldir}/mysql/bin/mysql /usr/sbin/mysql/etc/init.d/mysqld start#${mysqldir}/mysql/bin/mysqladmin -u root password 'passw0rd'#echo "-----init password of MySQL-----"OLD_PWD=cat ${mysqldir}/mysql/log/mysql.log | grep "password is generated" |awk '{print $NF}'${mysqldir}/mysql/bin/mysqladmin -u root -p${OLD_PWD} password 'admin@admin'echo "root password is: admin@admin"
使用docker部署
docker run -p 3306:3306 --name mysql-01 \-v /mydata/mysql/log:/var/log/mysql \-v /mydata/mysql/data:/var/lib/mysql \-v /mydata/mysql/conf:/etc/mysql/conf.d \-e MYSQL_ROOT_PASSWORD=root \--restart=always \-d mysql:5.7
使用kubesphere 部署
dockerhub镜像
docker端口映射
pod资源分配
环境变量
同步主机时区
添加存储卷
挂载配置文件(需要提前配置configMap)
configMap配置
mysql-conf
conf.d
[client]default-character-set=utf8mb4[mysql]default-character-set=utf8mb4[mysqld]init_connect='SET collation_connection = utf8mb4_unicode_ci'init_connect='SET NAMES utf8mb4'character-set-server=utf8mb4collation-server=utf8mb4_unicode_ciskip-character-set-client-handshakeskip-name-resolve
参考
集群搭建:https://blog.csdn.net/luoxiaoqiang_qq/article/details/116274413
HAProxy负载均衡:https://www.cnblogs.com/linkstar/p/6505766.html
