之前学习了最常用的二进制安装方式,其主要步骤为: 上传安装包 -- 安装系统依赖包 -- 解压缩 -- 创建数据/日志文件路径 -- 修改参数文件 -- 根据参数文件初始化数据库。
今天学习一下另外两种安装方式:rpm包安装 与 源码编译安装
一、rpm包 安装
该方式最大的特点就是简单,一条命令即可完成,缺点 是很不灵活,参数配置都是标准化的,不过我们可以安装完之后自己修改。
1、先到官网 mysql.com/downloads,选中指定的操作系统类型,获取对应的rpm包
会看到官网提供了一下几种类型的 rpm 包:
MySQL-client 客户端组件
MySQL-debuginfo 调试MySQL的组件
MySQL-devel 想针对于MySQL编译安装PHP等依赖于MySQL的组件包
MySQL-embedded MySQL的嵌入式版本
MySQL-server 共享库
MySQL-shared 共享库
MySQL-shared-dompat 为了兼容老版本的共享库
MySQL-test MySQL的测试组件(在线处理功能)
2、上传 rpm 包到服务器
3、使用 rpm 命令进行安装
rpm -ivh MySQL-server-community-###.rpm
rpm -ivh MySQL-client-community-###.rpm
补充一点 rpm 命令的知识:
-i 指的就是安装,有这个就够了
-h 使用符号#显示安装进度
-v 报告每一步操作的情况
二、 源码编译 安装
当你想要对 mysql 做一些定制化,那你只能进行源码编译安装了。 该方式缺点是操作时间较长,步骤相对比较复杂
1、到官网 https://dev.mysql.com/downloads/mysql/ 下载源码包上传到服务器
2、 安装gcc编译器
yum install gcc gcc-c++ -y
3、下载安装cmake编译器 http://www.cmake.org/download/
tar -zxf cmake-3.5.2.tar.gz
cd cmake-3.5.2/
./bootstrap && gmake && gmake install
4、安装ncurses
tar -zxf ncurses-5.9.tar.gz
cd ncurses-5.9/
./configure && make && make install
5、安装bison
tar -zxf bison-3.0.4.tar.gz
cd bison-3.0.4/
./configure && make && make install
6、安装boost
tar -zxf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost
7、创建mysql用户&用户组及目录
groupadd -r mysql && useradd -r -g mysql -s /bin/false -M mysql
mkdir /usr/local/mysql //创建目录
mkdir /opt/mysql3306/data //数据库目录
8、编译安装mysql
tar -zxf mysql-5.7.18.tar.gz
cd mysql-5.7.18/
cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/opt/mysql3306/data -DSYSCONFDIR=/opt/mysql3306 DDEFAULT_CHARSET=UTF8_mb4 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR/opt/mysql3306/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost
(
指定安装文件的安装路径时常用的选项:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 指定安装路径
-DMYSQL_DATADIR=/data/mysql 数据安装路径
-DSYSCONFDIR=/etc 配置文件的安装路径
由于MySQL支持很多的存储引擎而默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项:
-DWITH_INNOBASE_STORAGE_ENGINE=1 安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 安装ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 安装BLACKHOLE存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 安装FEDERATED存储引擎
若要明确指定不编译某存储引擎,可以使用类似如下的选项:
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
比如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 不启用或不编译EXAMPLE存储引擎
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
如若要编译进其它功能,如SSL等,则可使用类似如下选项来实现编译时使用某库或不使用某库:
-DWITH_READLINE=1
-DWITH_SSL=system 表示使用系统上的自带的SSL库
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0
其它常用的选项:
-DMYSQL_TCP_PORT=3306 设置默认端口的
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock MySQL进程间通信的套接字的位置
-DENABLED_LOCAL_INFILE=1 是否启动本地的LOCAL_INFILE
-DEXTRA_CHARSETS=all 支持哪些额外的字符集
-DDEFAULT_CHARSET=utf8 默认字符集
-DDEFAULT_COLLATION=utf8_general_ci 默认的字符集排序规则
-DWITH_DEBUG=0 是否启动DEBUG功能
-DENABLE_PROFILING=1 是否启用性能分析功能
如果想清理此前的编译所生成的文件,则需要使用如下命令:
make clean
rm CMakeCache.txt
)
设置权限并初始化mysql
cd /opt/mysql3306/data
chown -R mysql:mysql /opt/mysql3306/data
5.7.18 开始不再有/usr/local/mysql/support-files/my-defaults.cnf(以前的默认配置文件)文件了,得自己创建
vi /opt/mysql3306/my3306.cnf
[mysqld_safe] #malloc-lib=/usr/local/lib malloc-lib=tcmalloc [mysqld] ######### Need modify according to the environment ######### server-id = 0 innodb_buffer_pool_size = 100M innodb_buffer_pool_instances = 2 innodb_log_file_size = 30M basedir = /usr/local/mysql datadir = /opt/mysql3306/data tmpdir = /opt/mysql3306/tmp log_bin = /opt/mysql3306/log/bin log-bin-index = /opt/mysql3306/log/bin.index relay_log = /opt/mysql3306/log/relay pid_file = /opt/mysql3306/data/mysql.pid socket = /opt/mysql3306/data/mysql.sock sql_mode = STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER lower_case_table_names = 1 optimizer_switch='use_index_extensions=off' # in 5.7,between MySQL 5.7.18 and 5.7.20,please set use_index_extensions=off. optimizer_switch='derived_merge=off' # in 5.7,prior to MySQL 5.7.20,please set derived_merge=off. #event_scheduler = ON # default OFF.no need to open if no scheduler task. #read_only = 1 # the SLAVE need to open for MHA. #log_bin_trust_function_creators = 1 # It controls whether stored function creators can be trusted not to create stored functions that will cause unsafe events to be written to the binary log.(default 0) innodb_numa_interleave = 1 # Enables the NUMA interleave memory policy for allocation of the InnoDB buffer pool. ######### General ######### skip_name_resolve log_slave_updates #skip-grant-tables #skip-networking #skip-slave-start user = mysql port = 3306 performance_schema = 1 character_set_server = utf8mb4 # If the version of the mysql server is 5.1,set default-character-set=utf8. autocommit = 1 max_connections = 1000 max_user_connections = 998 # should little the value of max_connections. max_connect_errors = 1000000 max_allowed_packet = 10M innodb_open_files = 512 # It specifies the maximum number of .ibd files that MySQL can keep open at one time. table_definition_cache = 512 # The number of table definitions (from .frm files) that can be stored in the definition cache.If innodb_open_files is set also, the highest setting is used. table_open_cache = 2048 # The number of open tables for all threads. table_open_cache_instances = 16 # A value of 8 or 16 is recommended on systems that routinely use 16 or more cores. innodb_stats_on_metadata = OFF # default OFF.When enabled,such as SHOW TABLE STATUS or accessing the INFORMATION_SCHEMA.TABLES or STATISTICS while updates non-persistent statistics(similar to ANALYZE TABLE.) innodb_stats_persistent = on innodb_stats_persistent_sample_pages = 64 # default 20.Increasing the value improves the accuracy of index statistics, which can improve the query execution plan. ######### timeout and lock ######### #connect_timeout = 10 # default 10.The number of seconds that the mysqld server waits for a connect packet before responding with Bad handshake. lock_wait_timeout = 3 # metadata lock. innodb_lock_wait_timeout = 5 # innodb row lock. innodb_autoinc_lock_mode = 2 # which allows higher concurrency for insert operations. wait_timeout = 1800 interactive_timeout = 1800 ######### Binlog ######### sync_binlog = 1 binlog_format = ROW binlog_cache_size = 1M # default 32768.for large transactions, increase the value.see Binlog_cache_use and Binlog_cache_disk_use status variables. expire_logs_days = 15 binlog_rows_query_log_events = 1 # write informational log events such as row query log events into its binary log ######### InnoDB ######### default_storage_engine = InnoDB default_tmp_storage_engine = InnoDB transaction_isolation = READ-COMMITTED #REPEATABLE-READ innodb_flush_method = O_DIRECT innodb_file_format = Barracuda # is deprecated and will be removed innodb_file_format_max = Barracuda # is deprecated and will be removed #innodb_data_file_path = ibdata1:12M:autoextend # default ibdata1:12M:autoextend. innodb_file_per_table = 1 #innodb_large_prefix = 1 # will be removed innodb_purge_threads = 4 # if the number of the core is more,the max value is 16. innodb_page_cleaners = 16 # the max value is 64,if the number of the core is more,can set to the same value as innodb_buffer_pool_instances. innodb_page_size = 16K # InnoDB page size (default 16KB). innodb_log_buffer_size = 16M innodb_log_files_in_group = 2 innodb_sort_buffer_size = 64M #innodb_support_xa = 1 # After 5.7.10,will can not Disabling innodb_support_xa.two-phase commit in XA transactions is always enabled. innodb_flush_log_at_trx_commit = 1 # default 1. innodb_strict_mode = 1 innodb_print_all_deadlocks = 1 innodb_flush_neighbors = 0 innodb_online_alter_log_max_size= 1G # Specifies an upper limit on the size of the temporary log files used during online DDL operations for InnoDB tables. innodb_buffer_pool_dump_at_shutdown = 1 innodb_buffer_pool_load_at_startup = 1 ######### Fusion-io ######### #innodb_adaptive_hash_index = on # default on. #innodb_adaptive_flushing = on # default on. #innodb_doublewrite = on # default on. innodb_io_capacity = 10000 # if is ssd,can be set to The count of iops. a disk may be 1000. innodb_io_capacity_max = 20000 # When configuring innodb_io_capacity_max, twice the innodb_io_capacity is often a good starting point. #innodb_old_blocks_time = 1000 # default 1000ms.Specifies how long in milliseconds a block inserted into the old sublist must stay there after its first access before it can be moved to the new sublist. #innodb_old_blocks_pct = 37 # default 37.Specifies the approximate percentage of the InnoDB buffer pool used for the old block sublist. innodb_thread_concurrency = 0 # default 0.innodb_thread_concurrency can be set smaller than the number of vCPUs. If the number of concurrent user threads for a workload is less than 64, set innodb_thread_concurrency=0. innodb_write_io_threads = 8 # simple can be set to The number of disk. innodb_read_io_threads = 8 # simple can be set to The number of disk. ######### Global ######### query_cache_type = 0 # default 0.no use QC. query_cache_size = 0 # no use. ######### Session ######### tmp_table_size = 64M sort_buffer_size = 32M join_buffer_size = 128M read_buffer_size = 16M read_rnd_buffer_size = 32M ######### Error-log ######### log_error = error.log log_error_verbosity = 3 # default 3(note warning error),2(warning error) ######### Slow-log ######### long_query_time = 1 slow_query_log = 1 log_slow_admin_statements = 1 log_slow_slave_statements = 1 min_examined_row_limit = 10001 # performance_schema_digests_size default is 10000.In order to MEM use this table to query data not record to slow.log. slow_query_log_file = slow.log log_queries_not_using_indexes = 1 # Whether queries that do not use indexes are logged to the slow query log. log_throttle_queries_not_using_indexes = 10 # limits the number of such queries per minute that can be written to the slow query log. A value of 0 (the default) means "no limit". ######### Replacation ######### log_timestamps = system slave_skip_errors = ddl_exist_errors slave_pending_jobs_size_max = 15M # The value must not be less than the master's value for max_allowed_packet which prevent a slave worker queue may become full. #super_read_only = NO # on the slave,if set it to Yes,there is a problem to cause OOM.default OFF. ######### Slave ######### enforce_gtid_consistency = 1 binlog_gtid_simple_recovery = 1 gtid_mode = ON master_info_repository = TABLE relay_log_info_repository = TABLE relay_log_recovery = 1 slave-parallel-type = LOGICAL_CLOCK slave-parallel-workers = 8 ######### Semi sync replication ######### #plugin_dir = /usr/local/mysql/lib/plugin/ plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so" rpl_semi_sync_master_enabled = 1 rpl_semi_sync_slave_enabled = 1 rpl_semi_sync_master_timeout = 3000 [mysqld-5.7] innodb_purge_rseg_truncate_frequency = 128 innodb_buffer_pool_dump_pct = 40 innodb_undo_log_truncate = on innodb_max_undo_log_size = 100M innodb_undo_logs = 128 innodb_undo_tablespaces = 3 slave_transaction_retries = 128 show_compatibility_56 = on # will be removed. slave_preserve_commit_order = 1 innodb_lru_scan_depth = 512 explicit_defaults_for_timestamp = 1 ######## PS ######## performance_schema_instrument = 'transaction=ON' # transaction performance_schema_instrument = 'wait/lock/metadata/sql/mdl=ON' # metadata performance_schema_instrument = 'memory/%=COUNTED' # memory performance_schema_consumer_events_statements_history_long = ON performance_schema_consumer_events_transactions_current = ON performance_schema_consumer_events_transactions_history = ON performance_schema_max_sql_text_length = 2048 # for MEM QUAN max_digest_length = 2048 # for MEM QUAN [mysql] user=root password=111111 socket = /opt/mysql3306/data/mysql.sock default-character-set = utf8 max_allowed_packet = 1G prompt = [\\u@\\h][\\d][\\r:\\m:\\s]>\\_ #prompt = [\\u@\\h@\\p][\\d][\\r:\\m:\\s]>\\_ no-auto-rehash #safe-updates # Permit only those UPDATE and DELETE statements that specify which rows to modify by using key values. |
mysqld --defaults-file=/opt/mysql3306/my3306.cnf --initialize --user=mysql --datadir=/opt/mysql3306/data
初始化报错:
mysqld: [Warning] World-writable config file '/opt/mysql3306/my3306.cnf' is ignored
配置文件的权限不可以是777,所以需进行修改
chmod 644 /opt/mysql3306/my3306.cnf
mysqld --defaults-file=/opt/mysql3306/my3306.cnf --initialize --user=mysql --datadir=/opt/mysql3306/data
启动数据库报错:
mysqld_safe no shared library for --malloc-lib=tcmalloc found in /usr/lib /usr/lib64 /usr/lib/i386-linux-gnu /usr/lib/x86_64-linux-gnu
这事因为我的参数文件中指定了内存分配方式为 tcmalloc ,所以需要安装tcmalloc
先安装 git 工具
yum install git
git clone https://github.com/gperftools/gperftools 或者登录 https://github.com/gperftools/gperftools 下载zip文件上传
unzip gperftools-master.zip
cd gperftools-master
sh autogen.sh
报错(缺少相关依赖包导致):
autogen.sh: line 3: autoreconf: command not found
安装依赖包
yum install autoconf automake libtool libunwind
sh autogen.sh
./configure
[warning]没有libunwind
查看INSTALL文件建议安装0.99版本的libunwind
http://download.savannah.gnu.org/releases/libunwind/ 下载对应版本上传
./configure
make # 时间比较长
make install
启动还是报错
mysqld_safe --defaults-file=/opt/mysql3306/my3306.cnf &
mysqld_safe --malloc-lib must be located in one of the directories: /usr/lib /usr/lib64 /usr/lib/i386-linux-gnu /usr/lib/x86_64-linux-gnu
创建软连接
ln -s /usr/local/lib/libtcmalloc.so /usr/lib/libtcmalloc.so
启动成功
mysqld_safe --defaults-file=/opt/mysql3306/my3306.cnf &
安装结束后优化mysql执行路径
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile