Ubuntu18.04编译安装mysql5.7.23

Posted maef

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ubuntu18.04编译安装mysql5.7.23相关的知识,希望对你有一定的参考价值。

本博客为源码安装mysql5.7.23案例全过程
环境ubuntu18.04
安装规划:
配置文件:/etc/my.cnf
数据目录:/r2/mysqldata
安装目录:/usr/local/mysql

1.下载mysql-boost
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-boost-5.7.23.tar.gz
2.安装工具
sudo apt-get install cmake -y

apt-get install gcc g++ -y

apt-get install git -y

apt-get install libncurses5 libncurses5-dev -y

sudo apt-get install -y build-essential

sudo apt-get install flex bison -y

sudo apt-get install mpi-default-dev libicu-dev python-dev libbz2-dev -y
3.下载并安装boost
3.1下载boost部署包
wget https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
3.2解压
tar -zxvf boost_1_59_0.tar.gz
3.3编译
cd /r2/soft/boost_1_59_0 && ./bootstrap.sh
最后部分显示如下证明编译成功
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... /usr
Generating Boost.Build configuration in project-config.jam...

Bootstrapping is done. To build, run:

./b2

To adjust configuration, edit ‘project-config.jam‘.
Further information:

  • Command line help:
    ./b2 --help

  • Getting started guide:
    http://www.boost.org/more/getting_started/unix-variants.html

  • Boost.Build documentation:
    http://www.boost.org/build/doc/html/index.html

3.4b2
cd /r2/soft/boost_1_59_0 && ./b2 -a -sHAVE_ICU=1 # the parameter means that it support icu or unicode #执行时间稍微长
正常执行最后结果如下:
gcc.compile.c++ bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/token_ids.o
gcc.compile.c++ bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/wave_config_constant.o
common.mkdir bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/cpplexer
common.mkdir bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/cpplexer/re2clex
gcc.compile.c++ bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/cpplexer/re2clex/aq.o
gcc.compile.c++ bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/cpplexer/re2clex/cpp_re.o
gcc.archive bin.v2/libs/wave/build/gcc-7/release/link-static/threading-multi/libboost_wave.a
common.copy stage/lib/libboost_wave.a
...updated 1127 targets...

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

/r2/soft/boost_1_59_0

The following directory should be added to linker library paths:

/r2/soft/boost_1_59_0/stage/lib

3.5安装
cd /r2/soft/boost_1_59_0 && sudo ./b2 install #这时就把包给装到/usr/local/include/boost目录下了
最后成功标志如下
common.copy /usr/local/lib/libboost_wave.so.1.59.0
ln-UNIX /usr/local/lib/libboost_wave.so
common.copy /usr/local/lib/libboost_exception.a
common.copy /usr/local/lib/libboost_system.a
common.copy /usr/local/lib/libboost_chrono.a
common.copy /usr/local/lib/libboost_timer.a
common.copy /usr/local/lib/libboost_test_exec_monitor.a
...updated 11816 targets...

4.创建目录
cd /r2 && mkdir mysqldata
cd /usr/local && mkdir mysql
5.预编译msyql
cd /r2/soft/mysql-5.7.23 && sudo cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/r2/mysqldata -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/r2/mysqldata/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLE_DOWNLOADS=1

成功的标志如下:
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -std=gnu++03 -DDBUG_OFF
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

WITH_MEMORY_STORAGE_ENGINE
WITH_READLINE

-- Build files have been written to: /r2/soft/mysql-5.7.23

注意:如果出现错误,再次编译的时候需要删除CMakeCache.txt文件

6.编译并安装
6.1编译
cd /r2/soft/mysql-5.7.23 && make -j4
成功的标志如下:
[ 98%] Building CXX object unittest/gunit/keyring/CMakeFiles/merge_keyring_file_tests-t.dir////plugin/keyring/file_io.cc.o
[100%] Building CXX object unittest/gunit/keyring/CMakeFiles/merge_keyring_file_tests-t.dir/
///plugin/keyring/buffer.cc.o
[100%] Building CXX object unittest/gunit/keyring/CMakeFiles/merge_keyring_file_tests-t.dir///__/plugin/keyring/hash_to_buffer_serializer.cc.o
[100%] Built target mysqld
Scanning dependencies of target udf_example
[100%] Building CXX object sql/CMakeFiles/udf_example.dir/udf_example.cc.o
[100%] Linking CXX shared module udf_example.so
[100%] Built target udf_example
[100%] Linking CXX executable merge_keyring_file_tests-t
[100%] Built target merge_keyring_file_tests-t
[100%] Linking CXX executable merge_large_tests-t
[100%] Built target merge_large_tests-t

6.2安装
cd /r2/soft/mysql-5.7.23 && make install

7.新建mysql组和用户并授权
groupadd mysql
useradd -g mysql mysql
mkdir /usr/local/mysql/data
chown -R mysql /usr/local/mysql
chgrp -R mysql /usr/local/mysql
chown -R mysql /r2/mysqldata
chgrp -R mysql /r2/mysqldata

8.添加mysql的环境变量
vim /etc/profile
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
让其生效source /etc/profile

9.初始化
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/r2/mysqldata
2018-10-04T09:36:02.519395Z 1 [Note] A temporary password is generated for [email protected]: p&Lta1fdsD,j
这个就是初始化密码:p&Lta1fdsD,j

10.优化便捷化操作
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
update-rc.d mysqld defaults

11.启动mysql
service mysqld start

12.登陆并创建账号
mysql -uroot -p 之后输入密码p&Lta1fdsD,j

set password=password("test123456");
grant insert,update,select,create,index,trigger,alter on . to [email protected]‘%‘ identified by ‘test123456‘;
grant insert,update,select,create,index,trigger,alter on . to [email protected]‘localhost‘ identified by ‘test123456‘;

flush privileges;

13.安装客户端
apt install mysql-client-core-5.7 -y
apt install mariadb-client-core-10.1 -y

注意:这时默认的配置什么都没有,位置/etc/mysql/my.cnf

14.编写脚本

启动脚本
启动脚本start.sh

!/bin/bash

/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/r2/mysqldata &

停止脚本:stop.sh

!/bin/bash

/usr/local/mysql/bin/mysqladmin -uroot -p shutdown &

15.优化配置
执行停掉数据库之后vim /etc/my.cnf之后执行启动./start.sh即可正常使用

16.my.cnf附录如下:

file_name :my.cnf

update :调整innodb_open_files设置值,必须小于open_files_limit的设置值

注意 :建议参数根据实际情况作调整

本配置文件主要适用于MySQL 5.7.23版本

********************************************

[client]
port = 3306
default-character-set = utf8
socket = /r2/mysqldata/mysql.sock

=======================================================================

# MySQL客户端配置

=======================================================================

[mysql]
prompt="([email protected]h) R:m:s [d]> "
no-auto-rehash
default-character-set = utf8

=======================================================================

MySQL服务器全局配置

=======================================================================

[mysqld]
user = mysql
port = 3306
server-id = 1
basedir = /usr/local/mysql
tmpdir = /r2/mysqldata
datadir = /r2/mysqldata
socket = /r2/mysqldata/mysql.sock
log-error=/r2/mysqldata/error.log
wait_timeout = 31536000

interactive_timeout = 600

sql_mode = #sql_mode 配置为空值

skip_name_resolve = 1

skip_name_resolve #开启dns解析
lower_case_table_names = 0
character-set-server = utf8
collation-server = utf8_unicode_ci

skip-character-set-client-handshake #忽略客户端的字符集,使用服务器的设置

auto_increment_increment = 1

auto_increment_offset = 1

log_timestamps = SYSTEM
init_connect= ‘SET NAMES utf8‘
max_allowed_packet = 128M

性能参数

open_files_limit = 10240
max_connections = 10000
max_user_connections=9990
max_connect_errors = 100000
table_open_cache = 1024
thread_cache_size = 64
max_heap_table_size = 32M
query_cache_type = 0

global cache

key_buffer_size = 64M
query_cache_size = 0
tmp_table_size = 32M #内存临时表
binlog_cache_size = 4M #二进制日志缓冲

session cache

sort_buffer_size = 8M #排序缓冲
join_buffer_size = 4M #表连接缓冲
read_buffer_size = 8M #顺序读缓冲
read_rnd_buffer_size = 8M #随机读缓冲
thread_stack = 256KB #线程的堆栈的大小

binlog设置

binlog_format = MIXED
log_bin = /r2/mysqldata/binlog
max_binlog_size = 1G
expire_logs_days = 3 #binlog比较占空间,注意磁盘空间
sync_binlog = 0 #重要参数必须修改为0

复制设置

log_slave_updates = 1

replicate-do-db = test

binlog-ignore-db = mysql

GTID 配置

gtid_mode=ON
enforce-gtid-consistency=true

****************** 开启并行复制(从库)******************

slave-parallel-type=LOGICAL_CLOCK #基于组提交的并行复制方式

slave-parallel-workers=8 #并行的SQL线程数量

master-info_repository=TABLE #master信息以表的形式保存

relay_log_info_repository=TABLE #slave信息以表的形式保存

relay_log_recovery=ON #relay_log自我修复

innodb

default_storage_engine = InnoDB

innodb_data_file_path = ibdata1:1G:autoextend

innodb_buffer_pool_size = 128M #系统内存50%
innodb_open_files = 5120
innodb_flush_log_at_trx_commit = 2 #线上服务器必须配置为2
innodb_file_per_table = 1
innodb_lock_wait_timeout = 5
innodb_io_capacity = 200 #根据您的服务器IOPS能力适当调整innodb_io_capacity,配SSD盘可调整到 10000 - 20000
innodb_io_capacity_max = 20000
innodb_flush_method = O_DIRECT
innodb_log_file_size = 2G
innodb_log_files_in_group = 2
innodb_large_prefix = 0
innodb_thread_concurrency = 64
innodb_strict_mode = OFF
innodb_sort_buffer_size = 4194304

****************** undolog设置 ******************

innodb_undo_directory = /r2/undolog #undolog日志目录

innodb_undo_tablespaces = 2 #undolog日志文件个数,mysql8之后将弃用

innodb_undo_logs = 128 #回滚段的数量, 至少大于等于35,默认128。

innodb_max_undo_log_size = 1G #当超过这个阀值(默认是1G),会触发truncate回收(收缩)动作,truncate后空间缩小到10M。

innodb_purge_rseg_truncate_frequency = 128 #控制回收(收缩)undolog的频率

innodb_undo_log_truncate = 1 #即开启在线回收undolog日志文件

log 设置

log_error = /r2/mysqldata/error.log
slow_query_log = 1
long_query_time = 10
slow_query_log_file = /r2/mysqldata/slow.log

=======================================================================

MySQL mysqldump配置

=======================================================================

[mysqldump]
quick
max_allowed_packet = 128M

=======================================================================

MySQL mysqld_safe配置

=======================================================================

[mysqld_safe]
log_error = /r2/mysqldata/error.log
pid_file = /r2/mysqldata/mysqldb.pid




























































































































































以上是关于Ubuntu18.04编译安装mysql5.7.23的主要内容,如果未能解决你的问题,请参考以下文章

Ubuntu18.04编译安装mysql5.7.23

ubuntu18.04 编译安装 apache php

ubuntu 18.04 64bit如何编译安装内核

WSL Ubuntu18.04 编译安装 nignx

Ubuntu18.04中Ceres库编译安装调用问题~

NS2基于ubuntu18.04