1. #/bin/bash
    2. # 1: 创建数据库安装目录,数据存放目录,日志目录,tmp目录
    3. mkdir -p /data/mysql3307/{data,logs,tmp}
    4. yum install -y libaio
    5. # 2: 下载官网mysql-5.7.22版本安装包
    6. wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
    7. # 3: 安装包移动到安装数据库文件路径
    8. mv mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz /data/mysql3307
    9. # 4: 切换到mysql安装路径
    10. cd /data/mysql3307/
    11. # 5: 解压mysql安装包
    12. tar -xvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
    13. # 6: 更改mysql文件夹名
    14. mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql-5.7.22
    15. # 7: 创建mysql用户
    16. useradd -s /bin/bash mysql
    17. # 8:切换到 /data/mysql3307/mysql-5.7.22目录
    18. cd /data/mysql3307/mysql-5.7.22
    19. # 9:创建mysql配置文件
    20. cat >/data/mysql3307/mysql-5.7.22/my.cnf <<EOF
    21. [client]
    22. port = 3307
    23. socket = /data/mysql3307/tmp/mysql.sock
    24. [mysql]
    25. prompt="\u@mysqldb \R:\m:\s [\d]> "
    26. no-auto-rehash
    27. [mysqld]
    28. user = mysql
    29. port = 3307
    30. basedir = /data/mysql3307/mysql
    31. datadir = /data/mysql3307/data
    32. socket = /data/mysql3307/tmp/mysql.sock
    33. pid-file = /data/mysql3307/data/mysqldb.pid
    34. character-set-server = utf8mb4
    35. skip_name_resolve = 1
    36. skip-slave-start
    37. open_files_limit = 65535
    38. back_log = 1024
    39. max_connections = 5000
    40. max_connect_errors = 1000000
    41. table_open_cache = 1024
    42. table_definition_cache = 1024
    43. table_open_cache_instances = 64
    44. thread_stack = 512K
    45. external-locking = FALSE
    46. max_allowed_packet = 32M
    47. sort_buffer_size = 4M
    48. join_buffer_size = 4M
    49. thread_cache_size = 7500
    50. query_cache_size = 0
    51. query_cache_type = 0
    52. interactive_timeout = 600
    53. wait_timeout = 600
    54. tmp_table_size = 32M
    55. max_heap_table_size = 32M
    56. slow_query_log = 1
    57. slow_query_log_file = /data/mysql3307/logs/slow3307.log
    58. log-error = /data/mysql3307/logs/error.log
    59. long_query_time = 0.1
    60. log_queries_not_using_indexes =1
    61. log_throttle_queries_not_using_indexes = 60
    62. min_examined_row_limit = 100
    63. log_slow_admin_statements = 1
    64. log_slow_slave_statements = 1
    65. server-id = 3307203
    66. log-bin = /data/mysql3307/logs/mybinlog
    67. sync_binlog = 1
    68. binlog_cache_size = 4M
    69. max_binlog_cache_size = 1G
    70. max_binlog_size = 1G
    71. expire_logs_days = 15
    72. master_info_repository = TABLE
    73. relay_log_info_repository = TABLE
    74. gtid_mode = on
    75. enforce_gtid_consistency = 1
    76. log_slave_updates
    77. binlog_format = row
    78. binlog_checksum = 1
    79. relay_log_recovery = 1
    80. relay-log-purge = 1
    81. key_buffer_size = 32M
    82. read_buffer_size = 8M
    83. read_rnd_buffer_size = 4M
    84. bulk_insert_buffer_size = 64M
    85. myisam_sort_buffer_size = 128M
    86. myisam_max_sort_file_size = 1G
    87. myisam_repair_threads = 1
    88. lock_wait_timeout = 3600
    89. explicit_defaults_for_timestamp = 1
    90. innodb_thread_concurrency = 0
    91. innodb_sync_spin_loops = 100
    92. innodb_spin_wait_delay = 30
    93. #slave
    94. replicate-wild-do-table=k8_server1_log.%
    95. replicate-wild-do-table=k8_server2_log.%
    96. transaction_isolation = REPEATABLE-READ
    97. #innodb_additional_mem_pool_size = 16M
    98. innodb_buffer_pool_size = 1G
    99. innodb_buffer_pool_instances = 4
    100. innodb_buffer_pool_load_at_startup = 1
    101. innodb_buffer_pool_dump_at_shutdown = 1
    102. innodb_data_file_path = ibdata1:1G:autoextend
    103. innodb_temp_data_file_path = ibtmp1:12M:autoextend:max:5G
    104. innodb_flush_log_at_trx_commit = 1
    105. innodb_log_buffer_size = 32M
    106. innodb_log_file_size = 1G
    107. innodb_log_files_in_group = 2
    108. innodb_max_undo_log_size = 1G
    109. #根据您的服务器IOPS能力适当调整
    110. #一般配普通SSD盘的话,可以调整到 10000 - 20000
    111. #配置高端PCIe SSD卡的话,则可以调整的更高,比如 50000 - 80000
    112. innodb_io_capacity = 3000
    113. innodb_io_capacity_max = 8000
    114. innodb_flush_neighbors = 0
    115. innodb_write_io_threads = 8
    116. innodb_read_io_threads = 8
    117. innodb_purge_threads = 4
    118. innodb_page_cleaners = 4
    119. innodb_open_files = 65535
    120. innodb_max_dirty_pages_pct = 50
    121. innodb_flush_method = O_DIRECT
    122. innodb_lru_scan_depth = 4000
    123. innodb_checksums = 1
    124. innodb_checksum_algorithm = crc32
    125. #innodb_file_format = Barracuda
    126. #innodb_file_format_max = Barracuda
    127. innodb_lock_wait_timeout = 10
    128. innodb_rollback_on_timeout = 1
    129. innodb_print_all_deadlocks = 1
    130. innodb_file_per_table = 1
    131. innodb_online_alter_log_max_size = 4G
    132. internal_tmp_disk_storage_engine = InnoDB
    133. innodb_stats_on_metadata = 0
    134. innodb_status_file = 1
    135. #注意: 开启 innodb_status_output & innodb_status_output_locks 后, 可能会导致log-error文件增长较快
    136. innodb_status_output = 0
    137. innodb_status_output_locks = 0
    138. #performance_schema
    139. performance_schema = 1
    140. performance_schema_instrument = '%=on'
    141. #innodb monitor
    142. innodb_monitor_enable="module_innodb"
    143. innodb_monitor_enable="module_server"
    144. innodb_monitor_enable="module_dml"
    145. innodb_monitor_enable="module_ddl"
    146. innodb_monitor_enable="module_trx"
    147. innodb_monitor_enable="module_os"
    148. innodb_monitor_enable="module_purge"
    149. innodb_monitor_enable="module_log"
    150. innodb_monitor_enable="module_lock"
    151. innodb_monitor_enable="module_buffer"
    152. innodb_monitor_enable="module_index"
    153. innodb_monitor_enable="module_ibuf_system"
    154. innodb_monitor_enable="module_buffer_page"
    155. innodb_monitor_enable="module_adaptive_hash"
    156. sql_mode=NO_ENGINE_SUBSTITUTION
    157. [mysqldump]
    158. quick
    159. max_allowed_packet = 32M
    160. EOF
    161. # 10:给数据库文件夹下所有授权myql管理
    162. chown -R mysql:mysql /data/mysql3307/
    163. # 11:初始化mysql
    164. ./bin/mysqld --defaults-file=/data/mysql3307/mysql-5.7.22/my.cnf --initialize --user=mysql --basedir=/data/mysql3307/mysql-5.7.22 --datadir=/data/mysql3307/data
    165. # 12:启动mysql
    166. bin/mysqld --defaults-file=/data/mysql3307/mysql-5.7.22/my.cnf --user=mysql --basedir=/data/mysql3307/mysql-5.7.22 --datadir=/data/mysql3307/data &
    167. # 13: 查看自动生成的随机密码
    168. # cat /data/mysql3307/logs/error.log
    169. # 14:登录mysql方式
    170. # /data/mysql3307/mysql-5.7.22/bin/mysql -uroot -p -S /data/mysql3307/tmp/mysql.sock
    171. # 15: 重置MySQL密码
    172. # alter user 'root'@'localhost' identified by 'hadoop';
    173. # flush privileges;
    174. # 16: 停止mysql服务
    175. #/data/mysql3307/mysql-5.7.22/bin/mysqladmin -uroot -p -S /data/mysql3307/tmp/mysql.sock shutdown