当前位置:   article > 正文

自建MySQL数据库搭建及配置_创建一个新的数据库,口令自定义

创建一个新的数据库,口令自定义

某智慧项目所有环境中,除购买了三个阿里云RDS外,还在ECS上以以下方式自建了4个数据库

一、自建数据库清单

环境级别

数据库地址

搭建方式

版本

用途

测试环境

10.70.238.247:3306

docker

5.6.51

唯一业务库

预发环境

10.100.10.203:3306

二进制

5.6.46

唯一业务库

正式环境

10.100.10.200:3306

docker

5.6.51

日志从库

正式环境

10.100.10.201:3306

docker

5.7.22

汇聚从库

二、测试数据库搭建

  1. ###测试环境使用docker的方式快速搭建一个MySQL数据库,搭建方式及配置文件如下
  2. [root@zhgd-middleware-test middleware]# pwd
  3. /alidata/middleware
  4. [root@zhgd-middleware-test middleware]# cat mysql.sh
  5. docker run -p 3306:3306 --name mysql_dev --restart always \
  6. -v /alidata/mysql/conf/my.cnf:/etc/mysql/my.cnf \
  7. -v /alidata/mysql/data:/usr/data \
  8. -v /alidata/mysql/logs:/usr/logs \
  9. -v /etc/localtime:/etc/localtime:ro \
  10. -e 'MYSQL_ROOT_PASSWORD=XXXXXXXXXX' \
  11. -d swr.cn-east-3.myhuaweicloud.com/syh/mysql:5.6.51
  12. [root@zhgd-middleware-test middleware]# cat /alidata/mysql/conf/my.cnf
  13. [client]
  14. port=3306
  15. [mysql]
  16. prompt=\\u@\\d \\r:\\m:\\s>
  17. no-auto-rehash
  18. default-character-set=utf8
  19. [mysqld]
  20. port=3306
  21. server-id=113 #主从是不一样的
  22. character-set-server=utf8
  23. skip-external-locking
  24. #skip-name-resolve
  25. max_connections=2000
  26. max_connect_errors=1000
  27. table_definition_cache=500
  28. table_open_cache=500
  29. sort_buffer_size = 4M
  30. tmp_table_size = 64M
  31. read_buffer_size = 1M
  32. read_rnd_buffer_size = 4M
  33. myisam_sort_buffer_size = 64M
  34. thread_cache_size = 8
  35. query_cache_type=0
  36. query_cache_size=0
  37. lower_case_table_names = 1
  38. log_bin_trust_function_creators = 1
  39. open_files_limit = 65535
  40. max_allowed_packet=1024M
  41. interactive_timeout=600
  42. wait_timeout=600
  43. #################slow log####################
  44. slow-query_log=1
  45. slow-query_log_file=/usr/logs/mysql.slow
  46. long_query_time=2
  47. ####################binlog######################
  48. log-bin=mysql-bin
  49. binlog-format=ROW
  50. expire_logs_days=10
  51. log-slave-updates=1
  52. ###############INNODB################
  53. sql_mode=''
  54. transaction-isolation=READ-COMMITTED
  55. #innodb_buffer_pool_size=4G #根据自己的内存配置,50%到70%之间
  56. innodb_buffer_pool_instances = 8
  57. innodb_flush_log_at_trx_commit=2
  58. innodb_flush_method=O_DIRECT
  59. innodb_max_dirty_pages_pct = 75
  60. innodb_open_files = 65535
  61. innodb_file_per_table=1
  62. innodb_io_capacity=2000

三、预发数据库搭建

  1. ###预发环境因为有数仓的调度验证,相对数据量较大,使用二进制搭建,搭建脚本如下
  2. [root@zhgd-mysql-pre tools]# cat mysql_install.sh
  3. #!/bin/bash
  4. #author:王海波
  5. #mysql5.6数据安装
  6. #20210610增加开机自启
  7. SoftDir=/usr/local/mysql
  8. DataDir=/alidata/mysql/data
  9. LogDir=/alidata/mysql/logs
  10. Port=3306
  11. mysqlInstall(){
  12. #关闭selinux
  13. setenforce 0
  14. sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  15. #关闭防火墙
  16. systemctl stop firewalld
  17. systemctl disable firewalld
  18. #安装依赖包
  19. yum -y install autoconf libaio
  20. #创建mysql用户
  21. useradd mysql
  22. #解压数据库软件
  23. tar zxvf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
  24. #移动软件目录
  25. mv mysql-5.6.46-linux-glibc2.12-x86_64 $SoftDir
  26. #创建数据目录
  27. mkdir -p $DataDir
  28. #创建日志目录
  29. mkdir -p $LogDir
  30. #权限
  31. chown -R mysql:mysql $DataDir
  32. chown -R mysql:mysql $LogDir
  33. #环境变量
  34. cat << EOF>> /etc/profile
  35. ##mysql
  36. export MYSQL_HOME=$SoftDir
  37. export MY_BASEDIR_VERSION=$SoftDir
  38. export PATH=$SoftDir/bin:\$PATH
  39. export LD_LIBRARY_PATH=$SoftDir/lib
  40. #####
  41. EOF
  42. #环境生效
  43. source /etc/profile
  44. #删除默认配置文件以防止发生错误
  45. rm -f /etc/my.cnf
  46. #mysql配置文件
  47. cp my.cnf $SoftDir
  48. #初始化数据库
  49. $SoftDir/scripts/mysql_install_db --basedir=$SoftDir --datadir=$DataDir
  50. #权限
  51. chown -R mysql:mysql $DataDir
  52. chown -R mysql:mysql $LogDir
  53. chown -R mysql:mysql $SoftDir
  54. #拷贝启动文件
  55. cp mysql.server /etc/init.d/mysqld
  56. #增加自动启动
  57. chmod +x /etc/init.d/mysqld
  58. #启动数据库
  59. /etc/init.d/mysqld start
  60. chkconfig --add mysqld
  61. chkconfig mysqld on
  62. #更新mysql的root密码
  63. $SoftDir/bin/mysqladmin -u root password 'XXXXXXXXXX'
  64. #创建用户并授权
  65. $SoftDir/bin/mysql -uroot -pXXXXXXXXXX -e "CREATE USER zzsa IDENTIFIED BY 'Pinming@1024';GRANT ALL PRIVILEGES on *.* to zzsa@'%';FLUSH PRIVILEGES;"
  66. }
  67. echo "--------数据库安装开始----"
  68. mysqlInstall
  69. source /etc/profile
  70. echo "--------数据库安装完成-----"
  71. ###配置文件如下
  72. [root@zhgd-mysql-pre tools]# cat my.cnf
  73. [client]
  74. port=3306
  75. [mysql]
  76. prompt=\\u@\\d \\r:\\m:\\s>
  77. no-auto-rehash
  78. default-character-set=utf8
  79. [mysqld]
  80. basedir=/usr/local/mysql
  81. datadir=/alidata/mysql/data
  82. port=3306
  83. #主从是不一样的
  84. server-id=101
  85. character-set-server=utf8
  86. skip-external-locking
  87. #skip-name-resolve
  88. max_connections=1000
  89. max_connect_errors=1000
  90. table_definition_cache=500
  91. table_open_cache=500
  92. sort_buffer_size = 4M
  93. tmp_table_size = 64M
  94. read_buffer_size = 1M
  95. read_rnd_buffer_size = 4M
  96. myisam_sort_buffer_size = 64M
  97. thread_cache_size = 8
  98. query_cache_type=0
  99. query_cache_size=0
  100. lower_case_table_names = 1
  101. log_bin_trust_function_creators = 1
  102. open_files_limit = 65535
  103. max_allowed_packet=1024M
  104. #################slow log####################
  105. slow-query_log=1
  106. slow-query_log_file=/alidata/mysql/logs/mysql.slow
  107. long_query_time=2
  108. ####################binlog######################
  109. log-bin=mysql-bin
  110. binlog-format=ROW
  111. expire_logs_days=10
  112. log-slave-updates=1
  113. ################INNODB################
  114. sql_mode=''
  115. transaction-isolation=READ-COMMITTED
  116. #根据自己的内存配置,50%到70%之间
  117. innodb_buffer_pool_size=16G
  118. innodb_buffer_pool_instances= 8
  119. innodb_flush_log_at_trx_commit=2
  120. innodb_flush_method=O_DIRECT
  121. innodb_max_dirty_pages_pct=75
  122. innodb_file_format=Barracuda
  123. innodb_log_files_in_group=3
  124. innodb_open_files=65535
  125. innodb_file_per_table=1
  126. innodb_io_capacity=2000

四、日志从库数据库搭建

  1. ###搭建方式如下,配置文件略,参考测试环境的配置文件
  2. [root@zhgd-mysql-log-slave-prod scripts]# cat mysql_slave.sh
  3. ###基于docker安全基线要求
  4. docker run -p 3306:3306 --name mysql_slave --restart always \
  5. -v /alidata/mysql/conf:/etc/mysql/conf.d \
  6. -v /alidata/mysql/logs:/var/log/mysql \
  7. -v /alidata/mysql/data:/var/lib/mysql \
  8. -v /alidata/mysql/localtime:/etc/localtime \
  9. -v /alidata/mysql/tmp:/tmp \
  10. -v /alidata/mysql/socket:/var/run/mysqld \
  11. --read-only \
  12. -e MYSQL_ROOT_PASSWORD=XXXXXXXXXX \
  13. -d swr.cn-east-3.myhuaweicloud.com/syh/mysql:5.6.51

五、汇聚从库数据库搭建

这里我们因为BI分析需要建立一个汇聚同步,考虑到新增库表时的自动同步需求,用cloudcanal、DTS等数据同步方式皆不合适,故使用了MySQL原生的多主一从的方式,主库为智慧工地生产的中台和业务RDS。下面详细交底搭建方式

1、使用脚本备份中台RDS

因为要应用多个备份,所以不适合使用物理备份方式,这里我们使用多线程逻辑备份工具mydumper,日常运维中可使用此脚本加入crontab中做自动备份

  1. #!/bin/bash
  2. source /etc/profile
  3. IP=zhgd-ztdb.mysql.rds.aliyuncs.com
  4. DATE=`date +%Y%m%d%H%M%S`
  5. USERNAME=ztsa
  6. PASSWORD=XXXXXXXXXX
  7. RMDIR=/alidata/backup/mysqlbackup/$IP/
  8. BACKUPDIR=/alidata/backup/mysqlbackup/$IP/data/$DATE
  9. PORT=3306
  10. LogFile=/alidata/tools/mysqlbackup-zt.log
  11. if [ ! -d "$BACKUPDIR" ]; then
  12. mkdir -p $BACKUPDIR
  13. fi
  14. echo -e "\033[36m ---------------------开始备份 $DATE -----------------\033[0m">>$LogFile
  15. /usr/local/bin/mydumper -h $IP -u $USERNAME -p $PASSWORD -P $PORT -k -t 4 -F 64 -c -o $BACKUPDIR
  16. if [ $? -eq 0 ]; then
  17. echo -e " \033[32m 三局复制中台库数据库 $i 自动备份成功\033[0m">>$LogFile
  18. #微信报警
  19. curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5c254a09-e588-4813-9c4c-87585b8e8f1f' \
  20. -H 'Content-Type: application/json' \
  21. -d '
  22. {
  23. "msgtype": "markdown",
  24. "markdown": {
  25. "content": "<font color=\"warning\">三局复制中台库数据库 '$i' 自动备份成功</font>"
  26. }
  27. }'
  28. else
  29. echo -e "\033[31m 三局复制中台库数据库 $i 备份失败\033[0m">>$LogFile
  30. #微信报警
  31. curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5c254a09-e588-4813-9c4c-87585b8e8f1f' \
  32. -H 'Content-Type: application/json' \
  33. -d '
  34. {
  35. "msgtype": "markdown",
  36. "markdown": {
  37. "content": "<font color=\"warning\">三局复制中台库数据库 '$i' 自动备份失败,请快速解决问题!!</font>"
  38. }
  39. }'
  40. fi
  41. if [ -d "$RMDIR" ]; then
  42. find /alidata/backup/mysqlbackup/$IP/data/ -mtime +1 -name "20*" -exec rm -rf {} \;
  43. fi
  44. echo -e "\033[36m ---------------------备份结束 ----------------- \033[0m">>$LogFile

2、使用脚本备份业务RDS

  1. #!/bin/bash
  2. source /etc/profile
  3. IP=zhgd-zyy.mysql.rds.aliyuncs.com
  4. DATE=`date +%Y%m%d%H%M%S`
  5. USERNAME=zzsa
  6. PASSWORD=XXXXXXXXXX
  7. RMDIR=/alidata/backup/mysqlbackup/$IP/
  8. BACKUPDIR=/alidata/backup/mysqlbackup/$IP/data/$DATE
  9. PORT=3306
  10. LogFile=/alidata/tools/mysqlbackup-zyy.log
  11. if [ ! -d "$BACKUPDIR" ]; then
  12. mkdir -p $BACKUPDIR
  13. fi
  14. echo -e "\033[36m ---------------------开始备份 $DATE -----------------\033[0m">>$LogFile
  15. /usr/local/bin/mydumper -h $IP -u $USERNAME -p $PASSWORD -P $PORT -k -t 4 -F 64 -c -o $BACKUPDIR
  16. if [ $? -eq 0 ]; then
  17. echo -e " \033[32m 三局复制业务库数据库 $i 自动备份成功\033[0m">>$LogFile
  18. #微信报警
  19. curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5c254a09-e588-4813-9c4c-87585b8e8f1f' \
  20. -H 'Content-Type: application/json' \
  21. -d '
  22. {
  23. "msgtype": "markdown",
  24. "markdown": {
  25. "content": "<font color=\"warning\">三局复制业务库数据库 '$i' 自动备份成功</font>"
  26. }
  27. }'
  28. else
  29. echo -e "\033[31m 三局复制业务库数据库 $i 备份失败\033[0m">>$LogFile
  30. #微信报警
  31. curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5c254a09-e588-4813-9c4c-87585b8e8f1f' \
  32. -H 'Content-Type: application/json' \
  33. -d '
  34. {
  35. "msgtype": "markdown",
  36. "markdown": {
  37. "content": "<font color=\"warning\">三局复制业务库数据库 '$i' 自动备份失败,请快速解决问题!!</font>"
  38. }
  39. }'
  40. fi
  41. if [ -d "$RMDIR" ]; then
  42. find /alidata/backup/mysqlbackup/$IP/data/ -mtime +1 -name "20*" -exec rm -rf {} \;
  43. fi
  44. echo -e "\033[36m ---------------------备份结束 ----------------- \033[0m">>$LogFile

3、清理掉主从不用的库表sql

  1. ###处理掉中台不要导进去的库表
  2. [root@zhgd-all-slave-prod tools]# cd /alidata/backup/mysqlbackup/zhgd-ztdb.mysql.rds.aliyuncs.com/data/20221203185701/
  3. [root@zhgd-all-slave-prod 20221203185701]# rm -f mysql*
  4. ###处理掉子应用不要导进去的库表
  5. [root@zhgd-all-slave-prod tools]# cd /alidata/backup/mysqlbackup/zhgd-zyy.mysql.rds.aliyuncs.com/data/20221203190301/
  6. [root@zhgd-all-slave-prod 20221203190301]# rm -f mysql* nacos* xxl-job* agilebpm.db_uploader*

4、将两个主库数据导入从库

  1. ###使用myloader将中台数据导入数据库
  2. [root@zhgd-all-slave-prod tools]# nohup myloader -h 10.100.10.201 -u root -p Cz#3Vf#je9ECNF3K -t 8 -o -d /alidata/backup/mysqlbackup/zhgd-ztdb.mysql.rds.aliyuncs.com/data/20221203185701/ &
  3. ###使用myloader将子应用数据导入数据库
  4. [root@zhgd-all-slave-prod tools]# nohup myloader -h 10.100.10.201 -u root -p Cz#3Vf#je9ECNF3K -t 8 -o -d /alidata/backup/mysqlbackup/zhgd-zyy.mysql.rds.aliyuncs.com/data/20221203190301/ &
  5. ###这一步要花比较长的时间
  6. [root@zhgd-all-slave-prod tools]# ps -ef | grep myloader
  7. root 1154 31023 4 19:35 pts/0 00:05:19 myloader -h 10.100.10.201 -u root -p Cz#3Vf#je9ECNF3K -t 8 -o -d /alidata/backup/mysqlbackup/zhgd-ztdb.mysql.rds.aliyuncs.com/data/20221203185701/
  8. root 2036 31023 2 21:00 pts/0 00:00:32 myloader -h 10.100.10.201 -u root -p Cz#3Vf#je9ECNF3K -t 8 -o -d /alidata/backup/mysqlbackup/zhgd-zyy.mysql.rds.aliyuncs.com/data/20221203190301/
  9. root 2315 31023 0 21:24 pts/0 00:00:00 grep --color=auto myloader

5、修改mysql配置文件

  1. ###数据库启动脚本如下
  2. [root@zhgd-all-slave-prod scripts]# cat mysql5.7_slave.sh
  3. docker run -it -d \
  4. --name mysql5.7_slave \
  5. --restart always -p 3306:3306 \
  6. -e MYSQL_ROOT_PASSWORD=XXXXXXXXXX \
  7. -v /alidata/mysql/conf/my.cnf:/etc/mysql/my.cnf \
  8. -v /alidata/mysql/data:/var/lib/mysql \
  9. -v /alidata/mysql/localtime:/etc/localtime \
  10. -v /alidata/mysql/log:/var/log/mysql \
  11. kurashitech/mysql5.7.22
  12. ###需要在my.cnf中增加以下配置,汇聚库不产生binlog
  13. ####################binlog######################
  14. #log-bin=OFF
  15. #binlog-format=ROW
  16. #expire_logs_days=3
  17. log-slave-updates=1
  18. #####忽略的库表添加上
  19. ####################slave######################
  20. replicate-ignore-db = mysql
  21. replicate-ignore-db = information_schema
  22. replicate-ignore-db = performance_schema
  23. replicate-ignore-db = nacos
  24. replicate-ignore-db = xxl-job
  25. replicate-ignore-db = sys
  26. replicate-ignore-table = agilebpm.db_uploader
  27. ###
  28. gtid_mode=on
  29. enforce_gtid_consistency=on
  30. master-info-repository = TABLE
  31. relay-log-info-repository = TABLE

6、mysql中做两主一从的配置

  1. ###查看中台RDS的binlog位置点,以master status信息为准
  2. [root@zhgd-all-slave-prod scripts]# cd /alidata/backup/mysqlbackup/zhgd-ztdb.mysql.rds.aliyuncs.com/data/20221203185701/
  3. [root@zhgd-all-slave-prod 20221203185701]# cat metadata
  4. Started dump at: 2022-12-03 18:57:01
  5. SHOW MASTER STATUS:
  6. Log: mysql-bin.000637
  7. Pos: 203751
  8. GTID:e699cc13-694b-11ed-a6cf-0c42a1e7eb4a:1-1005,
  9. e6d30918-694b-11ed-a6cf-0c42a18535a8:1-22308989
  10. SHOW SLAVE STATUS:
  11. Host: 10.112.250.106
  12. Log: mysql-bin.000638
  13. Pos: 182491
  14. GTID:e699cc13-694b-11ed-a6cf-0c42a1e7eb4a:1-1005,
  15. e6d30918-694b-11ed-a6cf-0c42a18535a8:1-22308989
  16. Finished dump at: 2022-12-03 19:15:35
  17. ###查看子应用RDS的binlog位置点,以master status信息为准
  18. [root@zhgd-all-slave-prod ~]# cd /alidata/backup/mysqlbackup/zhgd-zyy.mysql.rds.aliyuncs.com/data/20221203190301/
  19. [root@zhgd-all-slave-prod 20221203190301]# cat metadata
  20. Started dump at: 2022-12-03 19:03:01
  21. SHOW MASTER STATUS:
  22. Log: mysql-bin.000255
  23. Pos: 221996
  24. GTID:0bdb4617-6a1f-11ed-ac30-506b4bdd9df8:1-19820651
  25. SHOW SLAVE STATUS:
  26. Host: 11.200.216.72
  27. Log: mysql-bin.000257
  28. Pos: 215471
  29. GTID:0bdb4617-6a1f-11ed-ac30-506b4bdd9df8:1-19820651
  30. Finished dump at: 2022-12-03 20:55:23
  31. ###在MySQL中执行以下两个跟上主从复制的命令
  32. change master to master_host='zhgd-ztdb.mysql.rds.aliyuncs.com', master_user='ztsa', master_port=3306, master_password='XXXXXXXXXX', master_log_file='mysql-bin.000637', master_log_pos=203751 for channel 'master1';
  33. change master to master_host='zhgd-zyy.mysql.rds.aliyuncs.com', master_user='zzsa', master_port=3306, master_password='XXXXXXXXXX', master_log_file='mysql-bin.000255', master_log_pos=221996 for channel 'master2';
  34. start slave;
  35. ###至此,两主一从汇聚从库搭建完成
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/478016
推荐阅读
相关标签
  

闽ICP备14008679号