MySQL 编译相关选项配置和说明
[MySQL安装的根目录] -DCMAKE_INSTALL_PREFIX=/export/servers/mysql/ [MySQL数据库文件存放目录] -DMYSQL_DATADIR=/export/data/mysql/data/ [MySQL配置文件所在目录] -DSYSCONFDIR=/export/servers/mysql/etc/ [MySQL用户名] -DMYSQL_USER=mysql [MySQL的数据库引擎] -DWITH_MYISAM_STORAGE_ENGINE=1 [MySQL的数据库引擎] -DWITH_INNOBASE_STORAGE_ENGINE=1 [MySQL的数据库引擎] -DWITH_ARCHIVE_STORAGE_ENGINE=1 [MySQL的数据库引擎] -DWITH_MEMORY_STORAGE_ENGINE=1 [MySQL的readline library] -DWITH_READLINE=1 [MySQL的通讯目录] -DMYSQL_UNIX_ADDR=/export/data/mysql/tmp/mysql.sock [MySQL的监听端口] -DMYSQL_TCP_PORT=3358 [启用加载本地数据] -DENABLED_LOCAL_INFILE=1 [编译时允许自主下载相关文件] -DENABLE_DOWNLOADS=1 [允许使用分区] -DWITH_PARTITION_STORAGE_ENGINE=1 [使MySQL支持所有的扩展字符] -DEXTRA_CHARSETS=all [设置默认字符集为utf8] -DDEFAULT_CHARSET=utf8 [设置默认字符校对] -DDEFAULT_COLLATION=utf8_general_ci [禁用调试模式] -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 [通讯时支持ssl协议] -DWITH_SSL:STRING=bundled [允许使用zlib library] -DWITH_ZLIB:STRING=bundled
创建MySQL服务账号
## 检查和创建用户组和用户 cat /etc/passwd ##查看用户组列表 cat /etc/group ## 创建用户组mysql groupadd mysql ## 创建mysql用户并指定用户组mysql useradd -g mysql mysql
创建MySQL文件目录并授权
##创建mysql使用的文件夹 mkdir -p /export/data/mysql/tmp /export/data/mysql/data /export/servers/mysql/etc /export/data/mysql/dumps /export/data/mysql/log /home/mysql/scripts /home/mysql/logs /export/data/mysql/undo ##为mysql用户赋予该文件夹权限 chown -R mysql:mysql /export/data/mysql/
安装依赖包
##先安装bison-devel的rpm包 rpm -ivh bison-devel-2.4.1-5.el6.x86_64.rpm ##使用yum安装其他必要软件 yum -y install make gcc-c++ ncurses-devel perl-CPAN perl zlib-devel cmake
准备MySQL编译安装的boost
## 解压boost_1_59_0到/soft/boost_1_59_0目录下
## 注意不同版本MySQL可能要求不同版本的boost
tar -xvf boost_1_59_0.tar.bz2
最新源码下载:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
历史源码下载:https://downloads.mysql.com/archives/community/
下载到的源码包是rpm格式,在Windows下可以直接使用压缩文件解压,MySQL 5.7版本的源码包中包含一个最新版本源码包和与之匹配的boost包。
编译安装MySQL
## 进入MySQL 源码目录 ## 使用cmake来编译 cmake . -DWITH_BOOST=/soft/boost_1_59_0 -DCMAKE_INSTALL_PREFIX=/export/servers/mysql/ -DMYSQL_DATADIR=/export/data/mysql/data/ -DSYSCONFDIR=/export/servers/mysql/etc/ -DMYSQL_UNIX_ADDR=/export/data/mysql/tmp/mysql.sock -DMYSQL_TCP_PORT=3358 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DENABLE_DOWNLOADS=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled >> /home/mysql/logs/install-log ## 使用make && make install来编译和安装 make && make install
创建配置文件
[clent] port = 3358 socket = /export/data/mysql/tmp/mysql.sock [mysqld] port = 3358 socket = /export/data/mysql/tmp/mysql.sock datadir = /export/data/mysql/data/ #--- GLOBAL ---# log_timestamps = SYSTEM character-set-server = utf8 lower_case_table_names = 1 log-output = FILE log-error = /export/data/mysql/log/error.log #general_log #general_log_file = /export/data/mysql/log/mysql.log pid-file = /export/data/mysql/mysql.pid slow-query-log slow_query_log_file = /export/data/mysql/log/slow.log tmpdir = /export/data/mysql/tmp long_query_time = 0.1 sync_binlog = 1 log_timestamps = SYSTEM #--------------# #thread_concurrency = 16 thread_cache_size = 512 table_open_cache = 16384 table_definition_cache = 16384 sort_buffer_size = 4M join_buffer_size = 4M read_buffer_size = 4M read_rnd_buffer_size = 4M key_buffer_size = 64M myisam_sort_buffer_size = 64M tmp_table_size = 32M max_heap_table_size = 32M open_files_limit = 65535 query_cache_size = 0 query_cache_type = 0 bulk_insert_buffer_size = 64M binlog_rows_query_log_events =on sql_mode = STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION #--- NETWORK ---# back_log = 1024 max_allowed_packet = 256M interactive_timeout = 28800 wait_timeout = 28800 skip-external-locking max-connections = 1000 skip-name-resolve = 1 read_only = 0 #--- REPL ---# server-id = ${server_id} log-bin = mysql-bin master_info_repository = TABLE binlog_format = ROW expire_logs_days = 7 log_slave_updates = 1 slave-parallel-workers = 8 slave-parallel-type = LOGICAL_CLOCK slave_preserve_commit_order = 0 skip-slave-start gtid_mode = on enforce-gtid-consistency = true relay-log = relay-log relay_log_recovery = ON sync_relay_log = 0 relay_log_info_repository = TABLE #--- INNODB ---# default-storage-engine = INNODB innodb_data_home_dir = /export/data/mysql/data innodb_data_file_path = ibdata1:1024M:autoextend innodb_file_per_table innodb_log_group_home_dir = /export/data/mysql/data innodb_buffer_pool_size = ${innodb_buffer_pool_size}G #innodb_additional_mem_pool_size = 128M innodb_log_files_in_group = 3 innodb_log_file_size = 1024M innodb_log_buffer_size = 16M innodb_flush_log_at_trx_commit = 1 innodb_lock_wait_timeout = 120 innodb_flush_method = O_DIRECT innodb_max_dirty_pages_pct = 75 innodb_io_capacity = 1000 #innodb_thread_concurrency = 32 innodb_open_files = 65535 innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_print_all_deadlocks = 1 innodb_undo_directory = /export/data/mysql/undo innodb_undo_tablespaces = 4 innodb_purge_threads = 4 innodb_purge_batch_size = 400 innodb_stats_on_metadata = 0 [mysqldump] #quick max_allowed_packet = 256M [mysql] # auto-rehash # Remove the next comment character if you are not familiar with SQL # safe-updates default-character-set=utf8 [myisamchk] key_buffer_size = 128M sort_buffer_size = 128M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout
修改上面文件中变量server_id和innodb_buffer_pool_size的值,并将文件保存到export/servers/mysql/etc/my.cnf
初始化系统数据库
## 执行初始化配置脚本,创建系统自带的数据库和表 /export/servers/mysql/bin/mysqld --user=mysql --basedir=/export/servers/mysql/ --datadir=/export/data/mysql/data/ --initialize-insecure --initialize
参考链接:https://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html#option_mysql_install_db_defaults-file
启动MySQL服务
--启动数据库, 测试数据库是否能正常启动
/export/servers/mysql/bin/mysqld_safe --defaults-file=/export/servers/mysql/etc/my.cnf &
创建MySQL数据库账号
/export/servers/mysql/bin/mysql --socket="/export/data/mysql/tmp/mysql.sock" -uroot -e " ## 创建mysql_admin管理员账号 GRANT ALL PRIVILEGES ON *.* TO 'mysql_admin'@'%' IDENTIFIED BY 'mysql_admin' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* TO 'mysql_admin'@'localhost' IDENTIFIED BY 'mysql_admin' WITH GRANT OPTION; ## 清理账号 SET @@session.binlog_format = 'statement'; DELETE FROM mysql.user WHERE user = ''; DELETE FROM mysql.user WHERE authentication_string = ''; SET @@session.binlog_format = 'row'; "
设置开机启动:
## 编辑文件 vi /etc/rc.d/rc.local ## 添加如下内容 /export/servers/mysql/bin/mysqld_safe --defaults-file=/export/servers/mysql/etc/my.cnf &
在CentOS7版本中,/etc/rc.d/rc.local的权限较低,需要给该文件增加可执行权限
chmod +x /etc/rc.d/rc.local
创建快速登录脚本
## 编辑登录脚本 vim /usr/local/bin/mysql_login ## 插入下面代码 #!/usr/bin/env bash mysql_bin="/export/servers/mysql/bin/mysql" mysql_user='mysql_admin' mysql_password='mysql_admin' mysql_host='127.0.0.1' mysql_port=3358 $mysql_bin --host="${mysql_host}" --port=${mysql_port} --user="${mysql_user}" --password="${mysql_password}" --safe-updates --select-limit=1000000 --init-command="set autocommit=0" --prompt="u@h:d>" ## 为登录脚本授权 chmod +x /usr/local/bin/mysql_login