欢迎您, 来到 宁时修博客.^_^

MySQL安装---2、源码编译安装

2016/11/07 言则行 MySQL 1448
Linux上MySQL的几种安装方式

   软件包下载地址:http://mirrors.163.com/mysql/Downloads/

                              http://mirrors.sohu.com/mysql/

            官方:https://dev.mysql.com/downloads/mysql/



一、安装依赖包

 yum install -y gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel \
 libaio libaio-devel perl-Data-Dumper bison-devel

  (PS:大概有这么些,编译的时候缺啥再装啥。。。)


二、创建MySQL守护进程用户

 useradd mysql -M -s /sbin/nologin


三、数据存放路径

 mkdir -p /opt/data
 chown -R mysql.mysql /opt/data


四、编译安装MySQL

 1、下载MySQL源码包

 cd /usr/local/src/
 wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.X.tar.gz
 tar xf mysql-5.6.X.tar.gz


 2、编译参数

  具体查看官方编译参数;列表如下:

Formats

Description

Default

Introduced

Removed

BUILD_CONFIG

Use same build   options as official releases




CMAKE_BUILD_TYPE

Type of build   to produce

RelWithDebInfo



CMAKE_CXX_FLAGS

Flags for C++   Compiler




CMAKE_C_FLAGS

Flags for C   Compiler




CMAKE_INSTALL_PREFIX

Installation   base directory

/usr/local/mysql



COMPILATION_COMMENT

Comment about   compilation environment




CPACK_MONOLITHIC_INSTALL

Whether   package build produces single file

OFF



DEFAULT_CHARSET

The default   server character set

latin1



DEFAULT_COLLATION

The default   server collation

latin1_swedish_ci



-DWITH_SYMVER16

Whether   libmysqlclient.so.18 contains both symver 16 and 18 symbols.

OFF

5.6.31


ENABLED_LOCAL_INFILE

Whether to   enable LOCAL for LOAD DATA INFILE

OFF



ENABLED_PROFILING

Whether to   enable query profiling code

ON



ENABLE_DEBUG_SYNC

Whether to   enable Debug Sync support

ON



ENABLE_DOWNLOADS

Whether to   download optional files

OFF



ENABLE_DTRACE

Whether to   include DTrace support




ENABLE_GCOV

Whether to   include gcov support


5.6.3


ENABLE_GPROF

Enable gprof   (optimized Linux builds only)

OFF

5.6.6


IGNORE_AIO_CHECK

With   -DBUILD_CONFIG=mysql_release, ignore libaio check

OFF

5.6.1


INNODB_PAGE_ATOMIC_REF_COUNT

Enable or   disable atomic page reference counting

ON

5.6.16


INSTALL_BINDIR

User   executables directory

PREFIX/bin



INSTALL_DOCDIR

Documentation   directory

PREFIX/docs



INSTALL_DOCREADMEDIR

README file   directory

PREFIX



INSTALL_INCLUDEDIR

Header file   directory

PREFIX/include



INSTALL_INFODIR

Info file   directory

PREFIX/docs



INSTALL_LAYOUT

Select   predefined installation layout

STANDALONE



INSTALL_LIBDIR

Library file   directory

PREFIX/lib



INSTALL_MANDIR

Manual page   directory

PREFIX/man



INSTALL_MYSQLSHAREDIR

Shared data   directory

PREFIX/share



INSTALL_MYSQLTESTDIR

mysql-test   directory

PREFIX/mysql-test



INSTALL_PLUGINDIR

Plugin   directory

PREFIX/lib/plugin



INSTALL_SBINDIR

Server   executable directory

PREFIX/bin



INSTALL_SCRIPTDIR

Scripts   directory

PREFIX/scripts



INSTALL_SECURE_FILE_PRIVDIR

secure_file_priv   default value

platform specific

5.6.34


INSTALL_SECURE_FILE_PRIV_EMBEDDEDDIR

secure_file_priv   default value for libmysqld


5.6.34


INSTALL_SHAREDIR

aclocal/mysql.m4   installation directory

PREFIX/share



INSTALL_SQLBENCHDIR

sql-bench   directory

PREFIX



INSTALL_SUPPORTFILESDIR

Extra support   files directory

PREFIX/support-files



MEMCACHED_HOME

Path to   memcached

[none]



MYSQL_DATADIR

Data directory




MYSQL_MAINTAINER_MODE

Whether to   enable MySQL maintainer-specific development environment

OFF



MYSQL_PROJECT_NAME

Windows/OS X   project name

3306

5.6.5


MYSQL_TCP_PORT

TCP/IP port   number

3306



MYSQL_UNIX_ADDR

Unix socket   file

/tmp/mysql.sock



ODBC_INCLUDES

ODBC includes   directory




ODBC_LIB_DIR

ODBC library   directory




OPTIMIZER_TRACE

Whether to   support optimizer tracing


5.6.3


SUNPRO_CXX_LIBRARY

Client link   library on Solaris 10+


5.6.20


SYSCONFDIR

Option file   directory




TMPDIR

tmpdir default   value


5.6.16


WITHOUT_SERVER

Do not build   the server

OFF



WITHOUT_xxx_STORAGE_ENGINE

Exclude   storage engine xxx from build




WITH_ASAN

Enable   AddressSanitizer

OFF

5.6.15


WITH_BUNDLED_LIBEVENT

Use bundled   libevent when building ndbmemcache

ON



WITH_BUNDLED_MEMCACHED

Use bundled   memcached when building ndbmemcache

ON



WITH_CLASSPATH

Classpath to   use when building MySQL Cluster Connector for Java. Default is an empty   string.




WITH_DEBUG

Whether to   include debugging support

OFF



WITH_DEFAULT_COMPILER_OPTIONS

Whether to use   default compiler options

ON

5.6.6


WITH_DEFAULT_FEATURE_SET

Whether to use   default feature set

ON

5.6.6


WITH_EDITLINE

Which   libedit/editline library to use

bundled

5.6.12


WITH_EMBEDDED_SERVER

Whether to   build embedded server

OFF



WITH_EMBEDDED_SHARED_LIBRARY

Whether to   build a shared embedded server library

OFF

5.6.17


WITH_ERROR_INSERT

Enable error   injection in the NDB storage engine. Should not be used for building binaries   intended for production.

OFF



WITH_EXTRA_CHARSETS

Which extra   character sets to include

all



WITH_INNODB_MEMCACHED

Whether to   generate memcached shared libraries.

OFF



WITH_LIBEDIT

Use bundled   libedit library

ON


5.6.12

WITH_LIBEVENT

Which libevent   library to use

bundled

5.6.6


WITH_LIBWRAP

Whether to   include libwrap (TCP wrappers) support

OFF



WITH_NDBCLUSTER

Build the NDB   storage engine; alias for WITH_NDBCLUSTER_STORAGE_ENGINE

ON



WITH_NDBCLUSTER_STORAGE_ENGINE

Build the NDB   storage engine

ON



WITH_NDBMTD

Build multi-threaded   data node.

ON



WITH_NDB_BINLOG

Enable binary   logging by default by mysqld.

ON



WITH_NDB_DEBUG

Produce a   debug build for testing or troubleshooting.

OFF



WITH_NDB_JAVA

Enable   building of Java and ClusterJ support. Enabled by default. Supported in MySQL   Cluster only.

ON



WITH_NDB_PORT

Default port   used by a management server built with this option. If this option was not   used to build it, the management server's default port is 1186.

[none]



WITH_NDB_TEST

Include NDB   API test programs.

OFF



WITH_NUMA

Set NUMA   memory allocation policy


5.6.27


WITH_READLINE

Use bundled   readline library

OFF


5.6.5

WITH_SSL

Type of SSL   support

bundled



WITH_UNIXODBC

Enable   unixODBC support

OFF



WITH_VALGRIND

Whether to   compile in Valgrind header files

OFF



WITH_ZLIB

Type of zlib   support

bundled



WITH_xxx_STORAGE_ENGINE

Compile   storage engine xxx statically into server





  大致整理如下:

-DCMAKE_INSTALL_PREFIX= 指向mysql安装目录
-DINSTALL_SBINDIR=sbin 指向可执行文件目录(prefix/sbin)
-DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录(/var/lib/mysql)
-DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)
-DINSTALL_PLUGINDIR=lib/mysql/plugin 指向插件目录(prefix/lib/mysql/plugin)
-DINSTALL_MANDIR=share/man 指向man文档目录(prefix/share/man)
-DINSTALL_SHAREDIR=share 指向aclocal/mysql.m4安装目录(prefix/share)
-DINSTALL_LIBDIR=lib/mysql 指向对象代码库目录(prefix/lib/mysql)
-DINSTALL_INCLUDEDIR=include/mysql 指向头文件目录(prefix/include/mysql)
-DINSTALL_INFODIR=share/info 指向info文档存放目录(prefix/share/info)
prefix官方推荐设为/usr
Storage Engine相关
类型csv,myisam,myisammrg,heap,innobase,archive,blackhole
若想启用某个引擎的支持:-DWITH_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
若想禁用某个引擎的支持:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
Library相关
-DWITH_READLINE=1 启用readline库支持(提供可编辑的命令行)
-DWITH_SSL=system 启用ssl库支持(安全套接层)
-DWITH_ZLIB=system 启用libz库支持(zib、gzib相关)
-DWTIH_LIBWRAP=0 禁用libwrap库(实现了通用TCP包装的功能,为网络服务守护进程使用)
-DMYSQL_TCP_PORT=3306 指定TCP端口为3306
-DMYSQL_UNIX_ADDR=/tmp/mysqld.sock 指定mysql.sock路径
-DENABLED_LOCAL_INFILE=1 启用本地数据导入支持
-DEXTRA_CHARSETS=all 启用额外的字符集类型(默认为all)
-DDEFAULT_CHARSET=utf8 指定默认的字符集为utf8
-DDEFAULT_COLLATION=utf8_general_ci 设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)
-DWITH_EMBEDDED_SERVER=1 编译嵌入式服务器支持
-DMYSQL_USER=mysql 指定mysql用户(默认为mysql)
-DWITH_DEBUG=0 禁用debug(默认为禁用)
-DENABLE_PROFILING=0 禁用Profiling分析(默认为开启)
-DWITH_COMMENT='string' 一个关于编译环境的描述性注释

  一些编译参数已经移除或者默认启用了,详情查看:官方编译参数


 3、开始编译

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.X \
-DMYSQL_DATADIR=/opt/data \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DSYSCONFDIR=/etc \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_FAST_MUTEXES=1 \
-DWITH_ZLIB=system \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_DEBUG=0
 
make
make install
cd /usr/local/
ln -s mysql-5.6.X/ mysql
chown -R mysql.mysql mysql-5.6.X/


五、配置my.cnf

[client]
port    = 3309
socket  = /tmp/mysql.sock
 
[mysql]
#prompt="(\u:HOSTNAME:)[\d]> "
prompt="\u@\h \R:\m:\s [\d]> "
no-auto-rehash
 
[mysqld]
user    = mysql
port    = 3309
socket  = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /opt/data
character-set-server = utf8
skip_name_resolve = 1
open_files_limit    = 10420
back_log = 500
max_connections = 512
max_connect_errors = 10000
table_open_cache = 512
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 51
query_cache_size = 0
query_cache_type = 0
tmp_table_size = 96M
max_heap_table_size = 8M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 16M
explicit_defaults_for_timestamp=true
interactive_timeout = 1000
wait_timeout = 1000
 
 
###slowlog/error-log###
slow_query_log = 1
slow_query_log_file = /opt/data/slow.log
log-error = /opt/data/error.log
long_query_time = 1
 
 
###binlog###
log-bin = /opt/data/mysql-bin
sync_binlog = 1
binlog_cache_size = 4M
max_binlog_cache_size = 8M
max_binlog_size = 254M
expire_logs_days = 7
binlog_format = row
 
###replication###
server-id = 1
master_info_repository = TABLE

 
###myisam####
key_buffer_size = 32M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover_options
 
 
####innodb####
lock_wait_timeout = 300
innodb_thread_concurrency = 36
 
transaction_isolation = REPEATABLE-READ
#innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 3534M
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_dump_at_shutdown = 1
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_flush_log_at_trx_commit = 1
innodb_log_buffer_size = 16M
innodb_log_file_size = 256M
innodb_log_files_in_group = 2
innodb_io_capacity = 2000
innodb_io_capacity_max = 4000
innodb_max_dirty_pages_pct = 50
innodb_flush_method = O_DIRECT
innodb_lock_wait_timeout = 10
innodb_rollback_on_timeout = 1
innodb_print_all_deadlocks = 1
innodb_file_per_table = 1
 
 
[mysqldump]
quick
max_allowed_packet = 32M

 注:这只是大致的参数,具体参数及值请结合业务配置。


六、初始化MySQL

  cd /usr/local/mysql/scripts/
  ./mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ \
  --datadir=/opt/data/ --user=mysql


七、设置MySQL启动

  cd /usr/local/mysql/support-files/
  cp mysql.server /etc/init.d/mysqld


八、设置mysql的命令PATH

 1、使用符号链接(软链接)

   ln -s /usr/local/mysql/bin/* /usr/local/bin/

 

 2、设置环境变量

   echo "export PATH=/usr/local/mysql/bin/:$PATH" >> /etc/profile
   source /etc/profile


九、设置库、man帮助等

   ln -s /usr/local/mysql/include /usr/include/mysql
   echo "/usr/local/mysql/lib" >/etc/ld.so.conf.d/mariadb-x86_64.conf
   ldconfig
   sed -ir '42 iMANPATH_MAP     /usr/local/bin          /usr/local/mysql/man' /etc/man_db.conf


十、启动MySQL并做安全设置

[root@test ~]# /etc/init.d/mysqld start
Starting MySQL....... SUCCESS!
[root@test ~]# /usr/local/mysql/bin/mysql_secure_installation
 
 
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):    有密码就输密码,无密码则为空
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
 
Set root password? [Y/n] Y   设置root密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] Y   移除匿名用户
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] Y  禁止root远程登录
 ... Success!
 
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] Y   移除test库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] Y  重启加载授权表
 ... Success!
 
 
 
 
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
 
Thanks for using MySQL!
 
 
Cleaning up...



点赞
说说你的看法

所有评论: (0)