CentOS7编译安装MariaDB 10.1

yum -y install gcc gcc-c++ autoconf cmake wget
wget https://downloads.mariadb.org/f/mariadb-10.1.19/source/mariadb-10.1.19.tar.gz
wget http://www.canonware.com/download/jemalloc/jemalloc-4.2.0.tar.bz2

编译安装Jemalloc:

tar jxvf jemalloc-4.2.0.tar.bz2
cd jemalloc-4.2.0/
./configure
make
make install
cd ../
ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib/libjemalloc.so.2
ln -s /usr/local/lib/libjemalloc.so.2 /usr/lib64/libjemalloc.so.2

 

编译安装MySQL:

创建mysql用户与用户组

/usr/sbin/groupadd mysql
/usr/sbin/useradd -g mysql mysql

编译安装MariaDB

tar zxvf mariadb-10.1.19.tar.gz
cd mariadb-10.1.19/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=complex -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DCMAKE_EXE_LINKER_FLAGS=\"-ljemalloc\" -DWITH_SAFEMALLOC=OFF -DMYSQL_DATADIR=/data0/mysql/3306/data -DMYSQL_USER=mysql
make
make install
如果编译出错将参数 -DWITH_EMBEDDED_SERVER=1 删除并执行 make clean 后重新编译

一些相关设置

ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
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/
mkdir -p /data0/mysql/3306/relaylog/
chown -R mysql:mysql /data0/mysql/

以mysql用户帐号的身份建立数据表

/usr/local/webserver/mysql/scripts/mysql_install_db --collation-server=utf8_general_ci --basedir=/usr/local/webserver/mysql --datadir=/data0/mysql/3306/data --user=mysql

创建my.cnf配置文件

vi /usr/local/webserver/mysql/my.cnf

输入以下内容(适合4G内存以上的独立服务器或云服务器)

# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# MySQL programs look for option files in a set of
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the \"--help\" option.

# The following options will be passed to all MySQL clients
[client]
character-set-server = utf8
port                = 3306
socket                = /tmp/mysql.sock


# The MySQL server
[mysqld]
character-set-server = utf8
replicate-ignore-db = mysql
replicate-ignore-db = test
replicate-ignore-db = information_schema
user                = mysql
port                = 3306
socket                = /tmp/mysql.sock
basedir         = /usr/local/webserver/mysql
datadir         = /data0/mysql/3306/data
log-error         = /data0/mysql/3306/mysql_error.log
pid-file         = /data0/mysql/3306/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections = 5000
max_connect_errors = 6000
table_cache = 512
external-locking = FALSE
key_buffer_size = 256M
max_allowed_packet = 32M
table_open_cache = 512
sort_buffer_size = 1M
join_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
thread_cache_size = 300
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
# Try number of CPU\'s*2 for thread_concurrency
thread_concurrency = 8
default-storage-engine = InnoDB
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 2

# binary logging is required for replication
log-slave-updates
log-bin=/data0/mysql/3306/binlog/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 1G

relay-log-index = /data0/mysql/3306/relaylog/relaylog
relay-log-info-file = /data0/mysql/3306/relaylog/relaylog
relay-log = /data0/mysql/3306/relaylog/relaylog
expire_logs_days = 30

interactive_timeout = 120
wait_timeout = 120

# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id        = 1

skip-name-resolve
#master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

# The replication master for this slave - required
#master-host     =   <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port     =  3306

# Uncomment the following if you are using InnoDB tables
innodb_data_home_dir = /data0/mysql/3306/data
innodb_data_file_path = ibdata1:256M:autoextend
#innodb_log_group_home_dir = /data0/mysql/3306/data
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
innodb_buffer_pool_size = 1G
innodb_additional_mem_pool_size = 16M
# Set .._log_file_size to 25 % of buffer pool size
innodb_log_file_size = 128M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 120
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_file_per_table = 1
#log-slow-queries = /data0/mysql/3306/slow.log
#long_query_time = 10

[mysqldump]
quick
max_allowed_packet = 32M

#[mysql]
#no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates

#[myisamchk]
#key_buffer_size = 256M
#sort_buffer_size = 256M
#read_buffer = 2M
#write_buffer = 2M

#[mysqlhotcopy]
#interactive-timeout
对于1G~2G内存的VPS主机而言,一般使用MySQL自带的配置文件即可
cp /usr/local/webserver/mysql/support-files/my-large.cnf /usr/local/webserver/mysql/my.cnf

安装服务脚本

cd support-files/
cp mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/init.d/mysqld
cd ../
make clean
cd ../

编辑服务脚本文件

vi /etc/init.d/mysqld

查找并修改以下变量内容

basedir=/usr/local/webserver/mysql
datadir=/data0/mysql/3306/data

加入启动项

chkconfig --add mysqld
chkconfig --level 345 mysqld on

启动MySQL服务

service mysqld start

设置数据库root用户密码

/usr/local/webserver/mysql/bin/mysqladmin password 12345678
@友情提醒:最后的密码就不要跟着复制了 - -!