MySQL部署

Posted obed

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL部署相关的知识,希望对你有一定的参考价值。

介质获取

介质下载地址:MySQL :: Download MySQL Community Server (Archived Versions)

本文中使用的为适用于Redhat 7的5.7.25的版本(5.7.25-el7-x86_64),其它版本类似。

本文介绍两种安装方法:rpm安装和tar安装,其它安装方案(例如yum、apt等),请参考官网

不同版本的不同操作系统,可用的安装包和安装方式不尽相同,请根据实际情况进行安装。(例如,5.7.25的mysql,centos7的有rpm包和tar包,而centos6只有rpm包,只能使用rpm的方式安装)

rpm安装使用介质名称:mysql-5.7.25-1.el7.x86_64.rpm-bundle.tar

tar安装使用介质名称:mysql-5.7.25-el7-x86_64.tar.gz

rpm安装

卸载旧版本mysql

使用rpm安装,需要卸载旧版本的mysql和相关组件(mariadb也要卸载)。

使用命令rpm -qa | grep mysql获取机器上已有的mysql相关组件,使用命令rpm -e --nodeps mysql***进行卸载,其中,--nodeps 参数会忽略其依赖关系

使用rpm安装

将tar包上传到服务器上,进行解包(tar -xvf *.tar)。解压出来四个文件:

mysql-community-common-5.7.25-1.el7.x86_64.rpm
mysql-community-libs-5.7.25-1.el7.x86_64.rpm
mysql-community-client-5.7.25-1.el7.x86_64.rpm
mysql-community-server-5.7.25-1.el7.x86_64.rpm

使用rpm命令进行安装:

rpm -ivh mysql-community-common-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.25-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.25-1.el7.x86_64.rpm

也可以先使用rpm安装server,rpm会警告缺少的组件,然后根据缺少的组件再一个个安装。

配置文件和启停

rpm安装的配置文件为/etc/my.cnf。

通过service或者systemctl进行操作:

service mysqld start
service mysqld stop
service mysqld restart
service mysqld status

tar安装

tar包的方式安装较为简单:将tar包上传到服务器上,使用tar进行解压(tar -zxvf ***.tar.gz),即可使用。

建议设置mysql的环境变量

export MYSQL_HOME=/app/mysql/mysql
export PATH=$PATH:$MYSQL_HOME/bin

启停

mysqld --defaults-file=/app/mysql/log/mysql_3306.cnf &
mysqladmin -uuser_have_shutdown_power -p -S /tmp/mysql.sock shutdown

配置文件可自由指定,但是在配置文件中,必须指出mysql组件的目录(basedir)

使用tar安装,首次启动前,需要初始化数据库,生成mysql相关的管理库(information_schema、mysql等)

mysqld --defaults-file=/app/mysql/log/mysql_3306.cnf --initialize
mysqld --defaults-file=/app/mysql/log/mysql_3306.cnf &

初始化后,会在日志文件中生成默认密码,用默认密码登录后,必须修改密码

set password for root@localhost=password("password");

配置文件举例

较简单的配置文件如下:

[mysqld]
port = 3306
socket = /tmp/mysql.sock

basedir = /app/mysql/mysql
datadir = /app/mysql/data
log_error = /app/mysql/log/error.log
pid-file = /app/mysql/log/mysql_3306.pid
bind_address = 0.0.0.0

[client]
port = 3306
socket = /tmp/mysql.sock

上面的配置文件,建议只用于个人测试,对稳定性、性能等要求较高的,可以参考下面的内容,同时,注意要根据实际情况修改

[mysqld]
port = 3306
socket = /tmp/mysql.sock

basedir = /app/mysql/mysql
datadir = /app/mysql/data
log_error = /app/mysql/log/error.log
pid-file = /app/mysql/log/mysql_3306.pid
# slow log
slow_query_log_file=/app/mysql/log/slow.log
slow_query_log=on
log_queries_not_using_indexes = 1
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_throttle_queries_not_using_indexes = 10
expire_logs_days = 7
long_query_time=1
min_examined_row_limit = 100
# binlog_rows_query_log_events = 1
log_bin_trust_function_creators = 1
# binlog_gtid_simple_recovery=1
log_timestamps=system
# show_compatibility_56=on

bind_address = 0.0.0.0

plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_slave_enabled = 1
rpl_semi_sync_master_enabled = 1
rpl_semi_sync_master_wait_point = after_sync
rpl_semi_sync_master_timeout =10000
rpl_semi_sync_master_wait_no_slave=0

# binlog
server_id = 1
log_bin = mysql-bin
log_bin_index = master-bin.index
relay_log = relay-bin
binlog_format=row
binlog_rows_query_log_events = 1
sync_binlog = 1
master_info_repository = TABLE
relay_log_info_repository = TABLE
relay_log_recovery = 1


slave_rows_search_algorithms = 'INDEX_SCAN,HASH_SCAN'
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers = 16
slave_preserve_commit_order=1
slave_transaction_retries=128
sync_relay_log=0
sync_relay_log_info=0
sync_master_info=0

event_scheduler = 1
slave_pending_jobs_size_max= 67108864

#GTID
gtid_mode = on
enforce_gtid_consistency = 1
log_slave_updates = 1
binlog_gtid_simple_recovery=1

innodb_file_per_table = 1
innodb_buffer_pool_size = 8G
innodb_write_io_threads = 16

#connection
interactive_timeout = 1800
wait_timeout = 1800
lock_wait_timeout = 1800
skip_name_resolve = 1
max_connections = 3000
max_connect_errors = 1000000

##table cache performance settings
table_open_cache = 4096
table_definition_cache = 4096
table_open_cache_instances = 64

# session memory settings
read_buffer_size = 16M
read_rnd_buffer_size = 32M
sort_buffer_size = 8M
tmp_table_size = 64M
join_buffer_size = 128M
thread_cache_size = 64

lower_case_table_names=1
sql_mode = "STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER"
autocommit = 1
transaction_isolation = READ-COMMITTED
explicit_defaults_for_timestamp = 1
max_allowed_packet = 67108864

#innodb settings
innodb_page_size = 16384
innodb_buffer_pool_size = 8G
innodb_buffer_pool_instances = 8
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_lru_scan_depth = 4096
innodb_lock_wait_timeout = 5
innodb_io_capacity = 10000
innodb_io_capacity_max = 20000
innodb_flush_method = O_DIRECT
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
innodb_undo_logs = 128
#innodb_undo_tablespaces = 3
innodb_flush_neighbors = 0
innodb_log_file_size = 2147483648
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16777216
innodb_purge_threads = 4
innodb_large_prefix = 1
innodb_thread_concurrency = 24
innodb_print_all_deadlocks = 1
innodb_strict_mode = 1
innodb_sort_buffer_size = 67108864
innodb_write_io_threads = 16
innodb_read_io_threads = 16
innodb_file_per_table = 1
innodb_stats_persistent_sample_pages = 64
innodb_autoinc_lock_mode = 2
innodb_online_alter_log_max_size=1G
innodb_open_files=4096
#new innodb settings #
innodb_buffer_pool_dump_pct = 40
innodb_page_cleaners = 16
innodb_undo_log_truncate = 1
innodb_max_undo_log_size = 2G
innodb_purge_rseg_truncate_frequency = 128


##thread pool
#thread_pool_max_threads=400
#thread_pool_idle_timeout=60
#thread_pool_size=64
#thread_handling=pool-of-threads
#thread_handling=one-thread-per-connection
#thread_pool_oversubscribe = 3

character_set_server=utf8mb4
collation-server=utf8mb4_bin

[client]
port = 3306
socket = /tmp/mysql.sock

该配置文件中包含主从复制的配置,配置的说明,可以参考:一些常见的MySQL配置

参考内容

Centos6.4安装配置mysql

以上是关于MySQL部署的主要内容,如果未能解决你的问题,请参考以下文章

网络云盘项目——Redis部署MySQL部署MySQL表设计

Docker应用部署

MySQLUbuntu 安装部署 MySQL

MySQLUbuntu 安装部署 MySQL

MySQLUbuntu 安装部署 MySQL

Centos7部署MySQL 5.7.26二进制版本