1. 安装依赖
  2. 解压压缩包
  3. 进入解压后的目录
  4. 安装mysql到 /usr/local/mysql
  5. 初始化数据
  6. 修改属主和属组
  7. 启动服务
  8. 登录

安装依赖

yum -y install gcc gcc-c++ ncurses ncurses-devel cmake

下载软件

cd /usr/local/src/
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24.tar.gz
wget http://nginx.org/download/nginx-1.15.5.tar.gz
wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxvf mysql-5.7.24.tar.gz
tar -zxvf nginx-1.15.5.tar.gz
tar -zxvf php-7.2.11.tar.gz
tar -zxvf boost_1_59_0.tar.gz    //这里注意只能是59的版本.

创建用户组和不能登录的用户

groupadd mysql
useradd -g mysql -s /sbin/nologin -M mysql

cmake

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/src/boost_1_59_0 \
-DSYSCONFDIR=/usr/local/mysql/etc \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLE_DTRACE=0 \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DEXTRA_CHARSETS=all \
-DCMAKE_EXE_LINKER_FLAGS='-ljemalloc'        //必须要先安装ljemalloc

回车, 如果失败, 解决报错, 然后删除 CMakeCache.txt 重来.

编译 && 安装

make && make install

如果失败, 可能会提示: c : internal compiler error: Killed (program cc1plus), 这个原因大概是因为内存不足, 所以可以添加交换内存

sudo dd if=/dev/zero of=/swapfile bs=64M count=16    //创建分区文件
mkswap /swapfile    //生成swap文件系统
swapon /swapfile    //激活

make clean    //清除编译
make && make install    //重新编译安装
...

swapoff /swapfile    //关闭
rm /swapfile    //删除

配置文件

将一下文本复制到 /etc/my.conf 中

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysql]
prompt="MySQL [\\d]> "
no-auto-rehash

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

basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/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 = 300

max_connections = 1000
max_connect_errors = 6000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 500M
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 = 8

query_cache_type = 1
query_cache_size = 8M
query_cache_limit = 2M

ft_min_word_len = 4

log_bin = mysql-bin
binlog_format = mixed
expire_logs_days = 7

log_error = /usr/local/mysql/data/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/data/mysql-slow.log

performance_schema = 0
explicit_defaults_for_timestamp

#lower_case_table_names = 1

skip-external-locking

default_storage_engine = InnoDB
#default-storage-engine = MyISAM
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 = 500M

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M
read_buffer = 4M
write_buffer = 4M

然后执行下面这段shell命令

#!/bin/sh
Mem=`free -m | awk '/Mem:/{print $2}'`

  sed -i "s@max_connections.*@max_connections = $((${Mem}/3))@" /etc/my.cnf
  if [ ${Mem} -gt 1500 -a ${Mem} -le 2500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 16@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 16M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 16M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 128M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 32M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 256@' /etc/my.cnf
  elif [ ${Mem} -gt 2500 -a ${Mem} -le 3500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 32@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 32M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 32M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 512M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 64M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 512@' /etc/my.cnf
  elif [ ${Mem} -gt 3500 ]; then
    sed -i 's@^thread_cache_size.*@thread_cache_size = 64@' /etc/my.cnf
    sed -i 's@^query_cache_size.*@query_cache_size = 64M@' /etc/my.cnf
    sed -i 's@^myisam_sort_buffer_size.*@myisam_sort_buffer_size = 64M@' /etc/my.cnf
    sed -i 's@^key_buffer_size.*@key_buffer_size = 256M@' /etc/my.cnf
    sed -i 's@^innodb_buffer_pool_size.*@innodb_buffer_pool_size = 1024M@' /etc/my.cnf
    sed -i 's@^tmp_table_size.*@tmp_table_size = 128M@' /etc/my.cnf
    sed -i 's@^table_open_cache.*@table_open_cache = 1024@' /etc/my.cnf
  fi

初始化

./bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

修改属主和属组

cd /usr/local/mysql
chown mysql:root -R ./

启动

/usr/local/mysql/support-files/mysql.server start

设置密码

/usr/local/mysql/bin/mysql -e "grant all privileges on *.* to root@'127.0.0.1' identified by \"password\" with grant option;"

/usr/local/mysql/bin/mysql -e "grant all privileges on *.* to root@'localhost' identified by \"password\" with grant option;"

登录

/usr/local/mysql/bin/mysql uroot -p
> grant all privileges on test.* to lujinkai@'%' identified by 'password';
> flush privileges;
> exit;

允许远程用户lujinkai连接test数据库, 注意先开放3306端口.

以后如果新建数据库,就使用命令新建数据库 create database xxx,然后赋予用户权限

grant all privileges on xxx.* to lujinkai@'%' identified by 'password';
flush privileges;

优化启动和mysql命令

cp /usr/local/mysql/support-files/mysql.server /ect/init.d/mysqld    
vi /ect/init.d/mysqld    //设置basedir 和 datadir 的值
chmod +x /etc/init.d/mysqld    
chkconfig --add mysqld    //添加到systemctl服务, 添加到系统服务
chkconfig mysqld on    //添加到开机启动.    
(如果是Ubuntu系统, 则: update-rc.d mysqld defaults)

vi ~/.bash_profile
alias mysql=/usr/local/mysql/bin/mysql        //将这行添加到 .bash_profile
source ~/.bash_profile         //重新加载.bash_profile 文件, 不然需要重新登录才能生效.

标签: none

添加新评论