MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品。MySQL 最流行的关系型数据库管理系统,在 WEB 应用方面MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。MySQL 软件采用了双授权政策,它分为社区版和商业版,其体积小、速度快、总体拥有成本低,并且开源.
下载安装包 MySQL 官网:https://www.mysql.com/cn/ mysql-8.0.29源码包:https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.29.tar.gz
获取最新版源码包 进入 mysql 官网,点击 Downloads,然后选择 MySQL Community (GPL) Downloads »
,如图所示 选择 MySQL Community Server
,如图所示: 分别选择 Source Code
与 All Operating Systems (Generic) (Architecture Independent)
,如图所示: 在新窗口中右键点击 No thanks, just start my download.
复制下载链接 下载并解压 以当前最新的 mysql 8.0.29 版本为例
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.29.tar.gz tar -zxvf mysql-boost-8.0.29.tar.gz cd mysql-8.0.29
因是国外资源下载可能过慢,可在后台下载,如下:
nohup wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-boost-8.0.29.tar.gz & # 后台下载 tail -f wget_log # 查看下载进度
安装 1. 创建 swap 分区(非必须) 如果你的服务器内存小于 4G
,建议先创建 swap 分区,否则编译过程可能因为内存不足而中断
# count 一般设置为物理内存的二倍,但最大不要超过2G,示例中设置了 2G dd if=/dev/zero of=/home/swap bs=1024 count=2048000 mkswap /home/swap swapon /home/swap # 设置开机自动挂起 vi /etc/fstab /home/swap swap swap defaults 0 0 # 将这行添加至文件末尾
2. 安装依赖文件 yum install cmake gcc gcc-c++ perl bison ncurses-devel gcc-toolset-11-gcc gcc-toolset-11-gcc-c++ gcc-toolset-11-binutils libtirpc-devel autoconf -y # 编译安装 rpcgen wget https://github.com/thkukuk/rpcsvc-proto/releases/download/v1.4.3/rpcsvc-proto-1.4.3.tar.xz tar xsf rpcsvc-proto-1.4.3.tar.xz cd rpcsvc-proto-1.4.3 ./configure make && make install
3. 编译 mysql 并安装 cmake -DCMAKE_INSTALL_PREFIX=/www/server/mysql -DMYSQL_DATADIR=/www/server/data -DMYSQL_USER=www -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_DEBUG=0 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DENABLED_LOCAL_INFILE=1 -DFORCE_INSOURCE_BUILD=1 -DWITH_BOOST=boost make # 需要相当长的时间,建议放后台运行 nohup make >> /root/make.log & make install
其中可根据需要自定义的内容如下
配置项 说明 DCMAKE_INSTALL_PREFIX 指定 mysql 的安装路径 DMYSQL_DATADIR 指定 mysql 数据储存路径(即表数据) DMYSQL_USER 指定运行 mysql 的用户 DDEFAULT_CHARSET 指定默认的字符集编码 DDEFAULT_COLLATION 指定默认的排序方式 DWITH_DEBUG 禁用 debug,1 为启用
使用 初始化数据库 为了避免第一次启动失败,建议在 mysql 安装目录创建 my.ini
并写入如下默认内容,详细配置说明可参考文章底部
[client ] port = 3306 socket = /tmp/mysql.sock [mysqld ] server-id = 1 port = 3306 user = www basedir = /www/server/mysql datadir = /www/server/data tmpdir = /tmp socket = /tmp/mysql.sock
如果在编译时自定义了安装路径,需要修改 /www/server/mysql/
与 /www/server/data/
为对应路径
# 初始化数据库 /www/server/mysql/bin/mysqld --initialize-insecure # 设置数据目录权限 chown -R www /www/server/data/ && chgrp -R www /www/server/data/ chown -R www /www/server/mysql/ && chgrp -R www /www/server/mysql/ # 创建软连接 ln -s /www/server/mysql/bin/mysql /sbin/ # 启动 /www/server/mysql/support-files/mysql.server start # 初始化数据库(根据需要选择即可) /www/server/mysql/bin/mysql_secure_installation
my.cnf 配置 mysql 5.6 以上版本就没有自带 my.cnf
文件了,我们需要手动创建,以下为常用配置说明(照抄自 https://www.jianshu.com/p/5f39c486561b ,略有修改),只取需要修改的部分添加至 my.cnf
即可,其中 innodb_buffer_pool_size
与 log_bin
需要重点关注
[client ] port = 3306 socket = /tmp/mysql.sock [mysqld ] server-id = 1 port = 3306 user = www basedir = /www/server/mysql datadir = /www/server/data tmpdir = /tmp socket = /tmp/mysql.sock transaction_isolation = REPEATABLE-READ default_storage_engine = InnoDB default_tmp_storage_engine = InnoDB internal_tmp_disk_storage_engine = InnoDB character-set-server = utf8mb4 collation-server = utf8mb4_general_ci lower_case_table_names = 1 max_connections = 10000 max_connect_errors = 10000 open_files_limit = 65535 interactive_timeout = 1800 wait_timeout = 1800 back_log = 900 max_allowed_packet = 128M tmp_table_size = 64M max_heap_table_size = 64M query_cache_type = 0 query_cache_size = 64M query_cache_limit = 2M query_cache_min_res_unit = 4kb binlog_cache_size = 1M key_buffer_size = 64M read_buffer_size = 262144 read_rnd_buffer_size = 1M sort_buffer_size = 1M join_buffer_size = 1M table_open_cache = 2000 table_definition_cache = 1400 table_open_cache_instances = 2 thread_cache_size = 16 thread_stack = 512k general_log = 0 general_log_file = /usr/local/mysql-5.7.21/log/mysql-general.log log_error_verbosity = 2 log_error = /usr/local/mysql-5.7.21/log/mysql-error.log slow_query_log = 1 long_query_time = 30 min_examined_row_limit = 100 log_throttle_queries_not_using_indexes = 0 slow_query_log_file = /www/wwwlogs/mysql/mysql-slow.log log-queries-not-using-indexes = 1 log_bin = /www/wwwlogs/mysql/mysql-bin.log expire_logs_days = 0 max_binlog_size = 1000M binlog_format = row innodb_page_cleaners = 4 innodb_page_size = 16384 innodb_buffer_pool_size = 512M innodb_buffer_pool_instances = 1 innodb_buffer_pool_chunk_size = 128M innodb_buffer_pool_load_at_startup = 1 innodb_buffer_pool_dump_at_shutdown = 1 innodb_lru_scan_depth = 1024 innodb_lock_wait_timeout = 60 innodb_io_capacity = 2000 innodb_io_capacity_max = 4000 innodb_flush_method = O_DIRECT innodb_file_per_table = 1 innodb_undo_directory = /usr/local/mysql-5.7.21/log innodb_undo_tablespaces = 0 innodb_undo_logs = 128 innodb_undo_log_truncate = 1 innodb_max_undo_log_size = 1G innodb_log_group_home_dir = /usr/local/mysql-5.7.21/log innodb_log_file_size = 128M innodb_log_files_in_group = 3 innodb_log_buffer_size = 16M innodb_flush_log_at_timeout = 1 innodb_flush_log_at_trx_commit = 1 innodb_open_files = 8192 innodb_read_io_threads = 4 innodb_write_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 4 innodb_old_blocks_pct = 37 innodb_old_blocks_time=1000 innodb_use_native_aio = 1 innodb_data_home_dir=/usr/local/mysql-5.7.21/data innodb_data_file_path = ibdata1:12M:autoextend explicit_defaults_for_timestamp = 1 [mysqldump ] quick max_allowed_packet = 16M [mysql ] auto-rehash socket = /tmp/mysql.sock