[2020.05更新]
#安装依赖包 yum -y install gcc gcc-c++ ncurses ncurses-devel cmake #下载源码包 wget https://www.percona.com/downloads/Percona-Server-5.7/Percona-Server-5.7.29-32/source/tarball/percona-server-5.7.29-32.tar.gz wget https://github.com/jemalloc/jemalloc/archive/5.2.1.tar.gz -O jemalloc-5.2.1.tar.gz wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
① 编译安装Jemalloc:
tar zxvf jemalloc-5.2.1.tar.gz cd jemalloc-5.2.1/ ./autogen.sh make make install cd ../ ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so.2
② 编译安装MySQL:
卸载默认安装的MariaDB
rpm -qa | grep mariadb mariadb-libs-5.5.65-1.el7.x86_64 #根据显示的版本卸载MariaDB rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
创建mysql用户与用户组
/usr/sbin/groupadd mysql /usr/sbin/useradd -g mysql mysql
编译安装Percona Server 5.7 (MySQL 5.7)
tar zxvf percona-server-5.7.29-32.tar.gz cd percona-server-5.7.29-32/ cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DMYSQL_UNIX_ADDR=/data0/mysql/3306/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DEXTRA_CHARSETS=complex -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF -DMYSQL_DATADIR=/data0/mysql/3306/data -DMYSQL_USER=mysql -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data0/software -DWITH_SYSTEMD=1 make make install make clean cd ../
③ 一些相关设置:
ln -s /usr/local/webserver/mysql/lib/libperconaserverclient.so.20 /usr/lib/libperconaserverclient.so.20 chmod +w /usr/local/webserver/mysql chown -R mysql:mysql /usr/local/webserver/mysql
创建MySQL数据库存放目录
mkdir -p /data0/mysql/3306/data/ mkdir -p /data0/mysql/3306/binlog/ chown -R mysql:mysql /data0/mysql/
添加环境变量
echo -e '\n\nexport PATH=/usr/local/webserver/mysql/bin:$PATH\n' >> /etc/profile && source /etc/profile
创建my.cnf配置文件
vi /usr/local/webserver/mysql/my.cnf
输入以下内容
#需要根据服务器硬件来设置,双核4G内存环境的my.cnf仅供参考 # The following options will be passed to all MySQL clients [client] port = 3306 socket = /data0/mysql/3306/mysql.sock default-character-set = utf8mb4 # The MySQL server [mysqld] port = 3306 socket = /data0/mysql/3306/mysql.sock basedir = /usr/local/webserver/mysql datadir = /data0/mysql/3306/data pid-file = /data0/mysql/3306/mysql.pid user = mysql bind-address = 0.0.0.0 server-id = 1 init-connect = 'SET NAMES utf8mb4' character-set-server = utf8mb4 skip-name-resolve #skip-networking back_log = 600 max_connections = 2000 max_connect_errors = 10000 open_files_limit = 65535 table_open_cache = 128 max_allowed_packet = 4M binlog_cache_size = 1M max_heap_table_size = 8M tmp_table_size = 16M read_buffer_size = 2M read_rnd_buffer_size = 8M sort_buffer_size = 8M join_buffer_size = 8M key_buffer_size = 4M thread_cache_size = 16 query_cache_type = 1 query_cache_size = 8M query_cache_limit = 2M ft_min_word_len = 4 log_bin = /data0/mysql/3306/binlog/mysql-bin binlog_format = mixed expire_logs_days = 30 binlog_cache_size = 8M max_binlog_cache_size = 2048M max_binlog_size = 1024M log_error = /data0/mysql/3306/mysql-error.log slow_query_log = 1 long_query_time = 1 slow_query_log_file = /data0/mysql/3306/mysql-slow.log performance_schema = 0 explicit_defaults_for_timestamp #lower_case_table_names = 1 skip-external-locking default_storage_engine = InnoDB innodb_file_per_table = 1 innodb_open_files = 500 innodb_buffer_pool_size = 64M innodb_write_io_threads = 4 innodb_read_io_threads = 4 innodb_thread_concurrency = 0 innodb_purge_threads = 1 innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 2M innodb_log_file_size = 32M innodb_log_files_in_group = 3 innodb_max_dirty_pages_pct = 90 innodb_lock_wait_timeout = 120 bulk_insert_buffer_size = 8M myisam_sort_buffer_size = 8M myisam_max_sort_file_size = 10G myisam_repair_threads = 1 interactive_timeout = 28800 wait_timeout = 28800 [mysqldump] quick max_allowed_packet = 16M [myisamchk] key_buffer_size = 8M sort_buffer_size = 8M read_buffer = 4M write_buffer = 4M
初始化数据库
/usr/local/webserver/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/webserver/mysql --datadir=/data0/mysql/3306/data
④ 配置MySQL服务脚本:
编辑服务脚本文件
vi /usr/local/webserver/mysql/usr/lib/systemd/system/mysqld.service #查找并修改以下变量内容 PIDFile=/data0/mysql/3306/mysql.pid --pid-file=/data0/mysql/3306/mysql.pid
安装服务脚本并加入启动项
cp /usr/local/webserver/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system systemctl enable mysqld
启动MySQL服务
systemctl start mysqld
设置数据库root用户密码
#友情提醒:最后的密码就不要跟着复制粘贴了 /usr/local/webserver/mysql/bin/mysqladmin password 12345678