1

实验环境

OS: debian_version_8.11 64位
CPU: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
Mem: 8GB
Kernel: 3.16.0-10-amd64
Mariadb: mariadb-10.2.30

简要说明

1、安装包内网中已下载好,此文档中不演示
2、所有的源码包都在/apps/apps_src/
3、所有的服务安装路径都在/apps/xxxxx
4、官网下载:https://downloads.mariadb.org/

编译安装过程
1、安装相关依赖包

apt update && apt install cmake g++ bison libncurses5 libncurses5-dev libevent-dev openssl libssl-dev autoconf

2、解压缩下载包

tar zxvf mariadb-10.2.30.tar.gz && cd mariadb-10.2.30/

3、创建运行mysql的用户

groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -s /bin/false -d /dev/null -M mysql

4、查看可编译的选项
官方参考链接:

https://dev.mysql.com/doc/internals/en/installation-layout-options.html

5、配置选项

cmake . \
-DCMAKE_INSTALL_PREFIX=/apps/mysql \
-DMYSQL_DATADIR=/apps/mysql/data \
-DMYSQL_UNIX_ADDR=/apps/mysql/lock/mysql.sock \
-DSYSCONFDIR=/etc/mysql \
-DWITHOUT_TOKUDB=1 \
-DMYSQL_USER=mysql \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1 \
-DWITHOUT_PARTITION_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DDEFAULT_CHARSET=utf8 \
-DWITH_EXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DENABLE_PROFILING=1 2> ../mariadb-10.2.30-configure.err

备注: 如果编译失败请先删除目录下CMakeCache.txt,然后重新编译执行。

配置选项说明:
-DCMAKE_INSTALL_PREFIX=/apps/mysql       #指定安装mysql根目录
-DMYSQL_DATADIR=/apps/mysql/data         #指定mysql数据库存放数据的目录
-DMYSQL_UNIX_ADDR=/apps/mysql/lock/mysql.sock   #UNIX套接字文件,MySQL的通讯目录
-DSYSCONFDIR=/etc/mysql                  #指定mysql配置文件目录
-DMYSQL_USER=mysql                       #mysql用户名    
-DWITHOUT_TOKUDB=1                       #禁用
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1       #禁用
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1     #禁用
-DWITHOUT_PARTITION_STORAGE_ENGINE=1     #禁用
-DWITH_MYISAM_STORAGE_ENGINE=1           #启用Myisam存储引擎
-DWITH_MEMORY_STORAGE_ENGINE=1           #启用Memory存储引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1         #启用innobase数据库引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1          #启用archive数据库引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1        #启用blackhole数据库引擎
-DWITH_READLINE=1                        #支持mysql的readline库,提供可编辑的命令行
-DWITH_SSL=system                        #mysql通讯时支持ssl协议,(安全套接层)
-DWITH_ZLIB=system                       #支持允许使用zlib库,(安全套接层)
-DWITH_LIBWRAP=0                         #关闭libwrap库,(实现了通用TCP包装的功能,为网络服务守护进程使用)
-DMYSQL_TCP_PORT=3306                    #指定mysql的监听的端口
-DENABLED_LOCAL_INFILE=1                 #允许从本地导入数据,启用加载本地数据
-DDEFAULT_CHARSET=utf8                   #设置mysql默认使用utf8字符集,如不指定默认使用latin1西欧字符集
-DWITH_EXTRA_CHARSETS=all                #指定mysql扩展字符集支持所有的字符集。默认mysql支持所有字符集
-DDEFAULT_COLLATION=utf8_general_ci      #设定默认字符校对排序规则
-DWITH_DEBUG=0                           #禁用debug,默认为禁用
-DENABLE_PROFILING=1                     #启用Profiling分析,默认为开启

7.编译执行

make -j 4 2> ../mariadb-10.2.30-make.err

8.安装执行

make -j 4 install 2> ../mariadb-10.2.30-install.err

9.配置全局变量

echo "export PATH=$PATH:/apps/mysql/bin" >> /etc/profile.d/mysql.sh 
source /etc/profile.d/mysql.sh

10.进入安装目录检查目录并创建创建缺失的目录

mkdir -pv /apps/mysql/{lock,logs,run}

11.从安装目录中复制一份配置模板到/etc目录下

mkdir /etc/mysql
cp /apps/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf

12.根据生产环境设定配置文件(内存8G的my.cnf配置)
vim /etc/mysql/my.cnf

#The following options will be passed to all MySQL clients
clients
[client]
port        = 3306
socket        = /apps/mysql/lock/mysql.sock

#The MySQL server
[mysqld]
port        = 3306
socket        = /apps/mysql/lock/mysql.sock
basedir        = /apps/mysql
datadir        = /apps/mysql/data
pid-file    = /apps/mysql/run/mysql.pid
log-error    = /apps/mysql/logs/mysql-error.log
user        = mysql
bind-address=192.168.1.146
skip_name_resolve = 1
skip-external-locking
key_buffer_size = 384M
max_allowed_packet = 16M
table_open_cache = 512
back_log = 600
sort_buffer_size = 8M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
thread_cache_size = 64
query_cache_size = 32M
query_cache_limit = 2M
tmp_table_size = 64M
join_buffer_size = 64M
thread_concurrency = 4
ft_min_word_len = 4

explicit_defaults_for_timestamp = true
#skip-networking
max_connections = 1000
max_connect_errors = 1000
open_files_limit = 65535

transaction_isolation = REPEATABLE-READ
log-bin=mysql-bin
binlog_format=mixed
binlog_cache_size = 64M
max_heap_table_size = 8M
server-id    = 1
expire_logs_days = 30

default_storage_engine = InnoDB
innodb_file_per_table = 1
innodb_data_home_dir = /apps/mysql/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /apps/mysql/logs
innodb_buffer_pool_size = 4G
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_thread_concurrency = 0
innodb_purge_threads = 1
innodb_log_file_size = 1G
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_strict_mode = 0
innodb_log_buffer_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
interactive_timeout = 28800
wait_timeout = 28800

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 384M
sort_buffer_size = 256M
read_buffer = 128M
write_buffer = 128M

[mysqlhotcopy]
interactive-timeout

配置文件说明:
详情请看:MariaDB/MySQL配置文件my.cnf详解

13.根据my.cnf配置文件中目录设定mysql目录权限

chown mysql:mysql /apps/mysql/{data,logs,run,lock}

14.执行用户执行脚本, 安装数据库到数据库存放目录

/apps/mysql/scripts/mysql_install_db --user=mysql --datadir=/apps/mysql/data

15.启动mysqld服务

/apps/mysql/support-files/mysql.server start
ps aux | grep mysql

备注:如果启动有报错,可以看下日志报错信息。

tail -n20 /apps/mysql/logs/mysql-error.log

16.执行该命令提高数据库安全性,要先启动mysql服务,主要是用来设定root密码、删除匿名用户、取消远程链接、删除测试数据库等。

/apps/mysql/bin/mysql_secure_installation

17.登录mysql服务器

mysql -uroot -ptest123

18.编写mysql服务脚本加入systemd服务
vim /etc/systemd/system/mysqld.service

[Unit]
Description=MariaDB Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
User=mysql
Group=mysql
PIDFile=/apps/mysql/run/mysql.pid
ExecStart=/apps/mysql/support-files/mysql.server start
ExecReload=/apps/mysql/support-files/mysql.server reload
ExecStop=/apps/mysql/support-files/mysql.server stop

[Install]
WantedBy=multi-user.target

#按 Esc 键退出编辑模式,输入 :wq 保存并关闭mysqld.service文件。

19.赋予权限设定开机自启动

chmod 754 /etc/systemd/system/mysqld.service
systemctl start mysqld && systemctl enable mysqld

zhouwentaovip
12 声望2 粉丝