直接在机器上部署

安装遇到问题

Downloading packages:Delta RPMs disabled because /usr/bin/applydeltarpm not installed.

解决:
yum install -y deltarpm

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、安装、启动

  1. #由于CentOS7的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。
  2. #下载命令:
  3. cd /usr/local
  4. wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm --no-check-certificate
  5. #然后进行yum源的安装:
  6. rpm -ivh mysql57-community-release-el7-9.noarch.rpm
  7. rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
  8. #安装完成后,就可以使用yum命令安装mysql了:
  9. yum -y install mysql-server
  10. #启动mysql:
  11. systemctl restart mysqld
  12. #查看mysql状态:
  13. systemctl status mysqld

配置配置文件my.cnf

vim /etc/my.cnf

  1. [mysqld]
  2. datadir=/var/lib/mysql
  3. socket=/var/lib/mysql/mysql.sock
  4. # Disabling symbolic-links is recommended to prevent assorted security risks
  5. symbolic-links=0
  6. # Settings user and group are ignored when systemd is used.
  7. # If you need to run mysqld under a different user or group,
  8. # customize your systemd unit file for mariadb according to the
  9. # instructions in http://fedoraproject.org/wiki/Systemd
  10. [mysqld_safe]
  11. log-error=/var/log/mariadb/mariadb.log
  12. pid-file=/var/run/mariadb/mariadb.pid
  13. #
  14. # include all files from the config directory
  15. #
  16. !includedir /etc/my.cnf.d
  17. [mysqld]
  18. user=mysql
  19. server-id = 1
  20. port = 3306
  21. datadir=/var/lib/mysql/mysql_data
  22. tmpdir = /tmp
  23. skip-external-locking
  24. skip_name_resolve = 1
  25. lower_case_table_names = 1
  26. max_connections = 8000
  27. max_connect_errors = 8000
  28. explicit_defaults_for_timestamp = 1
  29. max_allowed_packet = 256M
  30. interactive_timeout = 1800
  31. wait_timeout = 1800
  32. tmp_table_size = 128M
  33. max_heap_table_size = 128M
  34. query_cache_size = 0
  35. query_cache_type = 0
  36. read_buffer_size = 1M
  37. read_rnd_buffer_size = 2M
  38. binlog_cache_size = 8M
  39. join_buffer_size = 128M
  40. back_log = 600
  41. explicit_defaults_for_timestamp = false
  42. query_cache_wlock_invalidate = OFF
  43. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  44. #lc-messages-dir=/usr/share/mysql/english
  45. #######log set#############
  46. log_error = /var/lib/mysql/log/mysql.log
  47. slow_query_log = 1
  48. slow_query_log_file = /var/lib/mysql/log/slow.log
  49. log_queries_not_using_indexes = 1
  50. long_query_time = 2
  51. expire_logs_days = 7
  52. log-bin=/var/lib/mysql/log/mysql-bin
  53. [client]
  54. default-character-set=utf8
  55. [mysql]
  56. 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

  1. #!/bin/bash
  2. echo "############################# MySQL installing..........########################"
  3. echo "-----config my.cnf-----"
  4. mv /etc/my.cnf /etc/my.cnf_date +%Y%m%d
  5. SERVER_ID=ifconfig eth0 |grep inet |awk '{print $2}'|awk -F. '{print $3$4}'
  6. Mem=free -m |grep Mem |awk '{print $2}'
  7. SIZE=$[Mem*1/2]
  8. mysqldir=pwd
  9. cat >>/etc/my$ipaddr.cnf<<EOF
  10. [mysqld]
  11. user=mysql
  12. server-id = 1
  13. port = 3306
  14. basedir=${mysqldir}/mysql
  15. datadir=${mysqldir}/mysql/mysql_data
  16. tmpdir = /tmp
  17. skip-external-locking
  18. skip_name_resolve = 1
  19. lower_case_table_names = 1
  20. max_connections = 8000
  21. max_connect_errors = 8000
  22. explicit_defaults_for_timestamp = 1
  23. max_allowed_packet = 256M
  24. interactive_timeout = 1800
  25. wait_timeout = 1800
  26. tmp_table_size = 128M
  27. max_heap_table_size = 128M
  28. query_cache_size = 0
  29. query_cache_type = 0
  30. read_buffer_size = 1M
  31. read_rnd_buffer_size = 2M
  32. binlog_cache_size = 8M
  33. join_buffer_size = 128M
  34. back_log = 600
  35. explicit_defaults_for_timestamp = false
  36. query_cache_wlock_invalidate = OFF
  37. sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
  38. #######log set#############
  39. log_error = ${mysqldir}/mysql/log/mysql.log
  40. slow_query_log = 1
  41. slow_query_log_file = ${mysqldir}/mysql/log/slow.log
  42. log_queries_not_using_indexes = 1
  43. long_query_time = 2
  44. expire_logs_days = 7
  45. log-bin=${mysqldir}/mysql/log/mysql-bin
  46. [client]
  47. default-character-set=utf8
  48. [mysql]
  49. default-character-set=utf8
  50. EOF
  51. echo "-----create group and user:mysql-----"
  52. groupadd mysql
  53. useradd -r -g mysql -s /bin/false mysql
  54. echo "----- install dependent package -----"
  55. yum -y install numactl libaio lrzsz wget
  56. #echo "-----Download MySQL-----"
  57. #if [ -d /soft ];then
  58. echo "software dir is exist! Please check!"
  59. #else
  60. mkdir -p /soft
  61. #fi
  62. wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
  63. echo "-----Unzip MySQL-----"
  64. tar -zxvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar
  65. mv mysql-5.7.28-1.el7.x86_64.rpm-bundle mysql
  66. echo "-----create datadir and change owner of dir-----"
  67. mkdir -p mysql/mysql_data
  68. mkdir -p mysql/log
  69. chown -R mysql.mysql /etc/my.cnf
  70. chown -R mysql.mysql ${mysqldir}/mysql/
  71. chown -R mysql.mysql ${mysqldir}/mysql/mysql_data/
  72. echo "-----config profile-----"
  73. echo 'PATH=${mysqldir}/mysql/bin/:$PATH' >> /etc/profile
  74. source /etc/profile
  75. echo "-----initialize MySQL-----"
  76. ${mysqldir}/mysql/bin/mysqld --initialize --user=mysql --basedir=${mysqldir}/mysql --datadir=${mysqldir}/mysql/mysql_data/
  77. echo "-----start MySQL-----"
  78. cp ${mysqldir}/mysql/support-files/mysql.server /etc/init.d/mysqld
  79. ln -s ${mysqldir}/mysql/bin/mysql /usr/sbin/mysql
  80. /etc/init.d/mysqld start
  81. #${mysqldir}/mysql/bin/mysqladmin -u root password 'passw0rd'
  82. #echo "-----init password of MySQL-----"
  83. OLD_PWD=cat ${mysqldir}/mysql/log/mysql.log | grep "password is generated" |awk '{print $NF}'
  84. ${mysqldir}/mysql/bin/mysqladmin -u root -p${OLD_PWD} password 'admin@admin'
  85. echo "root password is: admin@admin"

使用docker部署

  1. docker run -p 3306:3306 --name mysql-01 \
  2. -v /mydata/mysql/log:/var/log/mysql \
  3. -v /mydata/mysql/data:/var/lib/mysql \
  4. -v /mydata/mysql/conf:/etc/mysql/conf.d \
  5. -e MYSQL_ROOT_PASSWORD=root \
  6. --restart=always \
  7. -d mysql:5.7

使用kubesphere 部署

dockerhub镜像

mysql:5.7.37

docker端口映射

3306/tcp; 33060/tcp
image.png

pod资源分配

image.png

环境变量

MYSQL_ROOT_PASSWORD root
image.png

同步主机时区

image.png

添加存储卷

/var/lib/mysql
image.png

挂载配置文件(需要提前配置configMap)

/etc/mysql/conf.d
image.png

configMap配置

mysql-conf
conf.d

  1. [client]
  2. default-character-set=utf8mb4
  3. [mysql]
  4. default-character-set=utf8mb4
  5. [mysqld]
  6. init_connect='SET collation_connection = utf8mb4_unicode_ci'
  7. init_connect='SET NAMES utf8mb4'
  8. character-set-server=utf8mb4
  9. collation-server=utf8mb4_unicode_ci
  10. skip-character-set-client-handshake
  11. skip-name-resolve

参考

集群搭建:https://blog.csdn.net/luoxiaoqiang_qq/article/details/116274413
HAProxy负载均衡:https://www.cnblogs.com/linkstar/p/6505766.html