直接在机器上部署
安装遇到问题
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/local
wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm --no-check-certificate
#然后进行yum源的安装:
rpm -ivh mysql57-community-release-el7-9.noarch.rpm
rpm --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/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-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.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[mysqld]
user=mysql
server-id = 1
port = 3306
datadir=/var/lib/mysql/mysql_data
tmpdir = /tmp
skip-external-locking
skip_name_resolve = 1
lower_case_table_names = 1
max_connections = 8000
max_connect_errors = 8000
explicit_defaults_for_timestamp = 1
max_allowed_packet = 256M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 128M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 1M
read_rnd_buffer_size = 2M
binlog_cache_size = 8M
join_buffer_size = 128M
back_log = 600
explicit_defaults_for_timestamp = false
query_cache_wlock_invalidate = OFF
sql_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.log
slow_query_log = 1
slow_query_log_file = /var/lib/mysql/log/slow.log
log_queries_not_using_indexes = 1
long_query_time = 2
expire_logs_days = 7
log-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/bash
echo "############################# MySQL installing..........########################"
echo "-----config my.cnf-----"
mv /etc/my.cnf /etc/my.cnf_date +%Y%m%d
SERVER_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=pwd
cat >>/etc/my$ipaddr.cnf<<EOF
[mysqld]
user=mysql
server-id = 1
port = 3306
basedir=${mysqldir}/mysql
datadir=${mysqldir}/mysql/mysql_data
tmpdir = /tmp
skip-external-locking
skip_name_resolve = 1
lower_case_table_names = 1
max_connections = 8000
max_connect_errors = 8000
explicit_defaults_for_timestamp = 1
max_allowed_packet = 256M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 128M
max_heap_table_size = 128M
query_cache_size = 0
query_cache_type = 0
read_buffer_size = 1M
read_rnd_buffer_size = 2M
binlog_cache_size = 8M
join_buffer_size = 128M
back_log = 600
explicit_defaults_for_timestamp = false
query_cache_wlock_invalidate = OFF
sql_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.log
slow_query_log = 1
slow_query_log_file = ${mysqldir}/mysql/log/slow.log
log_queries_not_using_indexes = 1
long_query_time = 2
expire_logs_days = 7
log-bin=${mysqldir}/mysql/log/mysql-bin
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
EOF
echo "-----create group and user:mysql-----"
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
echo "----- install dependent package -----"
yum -y install numactl libaio lrzsz wget
#echo "-----Download MySQL-----"
#if [ -d /soft ];then
echo "software dir is exist! Please check!"
#else
mkdir -p /soft
#fi
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
echo "-----Unzip MySQL-----"
tar -zxvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
mv mysql-5.7.28-1.el7.x86_64.rpm-bundle mysql
echo "-----create datadir and change owner of dir-----"
mkdir -p mysql/mysql_data
mkdir -p mysql/log
chown -R mysql.mysql /etc/my.cnf
chown -R mysql.mysql ${mysqldir}/mysql/
chown -R mysql.mysql ${mysqldir}/mysql/mysql_data/
echo "-----config profile-----"
echo 'PATH=${mysqldir}/mysql/bin/:$PATH' >> /etc/profile
source /etc/profile
echo "-----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/mysqld
ln -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=utf8mb4
collation-server=utf8mb4_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
参考
集群搭建:https://blog.csdn.net/luoxiaoqiang_qq/article/details/116274413
HAProxy负载均衡:https://www.cnblogs.com/linkstar/p/6505766.html