环境说明:
1. 系统环境: CentOS7-64位 内存:4GB 硬盘:20GB (VMWARE虚拟机)

2. MySQL版本: 5.7.19

--------------

1. 下载MySQL

    下载MySQL5.7.19源码包

    

        下载后上传至Linux服务器,这里把源码包放到/usr/local目录下。

2. 环境准备

2.1 安装编译需要用到的依赖软件

    

如果数据库使用多实例,还需要安装: libaio, libaio-devel, 另外,还可安装: bison-devel openssl-devel。

cmake加参数:-DWITH_SSL=yes时依赖openssl库)

2.2 添加mysql用户和用户组

    执行命令: sudo groupadd mysql && sudo useradd mysql -s /sbin/nologin -M -g mysql

3. 开始安装

3.1 预编译

    执行命令:

cd /usr/local/mysql-5.7.19 && sudo cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii -DENABLE_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLE_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0

出现如下错误,提示找不到boost库:

    

    这里再添加两个参数: -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>, boost路径填/usr/local/boost即可,它会在这个目录找,找不到就会自己下载到该目录下,如下所示:

    

    注意:
    cmake生成的临时文件是CMakeCache.txt,重新cmake时,最好删除上一个CMakeCache.txt文件,避免不必要的错误。

3.2 编译


    执行命令: sudo make 即可,注意,内存最好大于等于4G, 内存为2G可能会编译失败。编译完如下:


    



3.3 安装


    执行命令: sudo make install。安装完如下:


    


3.4 配置

3.4.1 目录授权

    

    另外,还需创建一个目录,命令: sudo mkdir /usr/local/mysql/tmp

3.4.2 初始化mysql的系统数据表

    命令: sudo /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data

    在Mysql5.7之前,初始化一般都是使用sripts目录下的mysql_install_db脚本,这里改为用mysqld了, 并且初始化后有一个默认的root随机密码:

    

3.4.3 创建配置文件

    这里没有my-default.cnf文件:

    

    我们在网站找一个,然后按我们的参数修改配置即可,命令:
sudo mv /etc/my.cnf /etc/my.cnf.bak && sudo mv my.cnf /etc/my.cnf,文件内容如下:

# Example MySQL config file for small systems.
#
# This is for a system with little memory (<= 64M) where MySQL is only used
# from time to time and it's important that the mysqld daemon
# doesn't use much resources.
#
# 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]
#password	= your_password
port		= 3298
socket		= /usr/local/mysql/tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port		= 3298
socket		= /usr/local/mysql/tmp/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 256K

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id	= 1

# Uncomment the following if you want to log updates
#log-bin=mysql-bin

# binary logging format - mixed recommended
#binlog_format=mixed

# Causes updates to non-transactional engines using statement format to be
# written directly to binary log. Before using this option make sure that
# there are no dependencies between transactional and non-transactional
# tables such as in the statement INSERT INTO t_myisam SELECT * FROM
# t_innodb; otherwise, slaves may diverge from the master.
#binlog_direct_non_transactional_updates=TRUE

# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /usr/local/mysql/data
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /usr/local/mysql/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 = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

[mysqldump]
quick
max_allowed_packet = 16M

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

[myisamchk]
key_buffer_size = 8M
sort_buffer_size = 8M

[mysqlhotcopy]
interactive-timeout



3.4.4 (按需)配置了SSL的需要执行

    命令: sudo /usr/local/mysql/bin/mysql_ssl_rsa_setup

3.5 启动

    命令: sudo /usr/local/mysql/bin/mysqld_safe --user=mysql &

    发生如下错误:

    

    

    修改目录授权:sudo chown -R mysql:mysql /usr/local/mysql,之前的所属用户和组是root,修改后重启启动:

    

    MySQL启动完成。

    前面在初始化系统数据表时,提示一句,说明了root的初始随机密码,内容如下:

    2017-07-30T14:39:57.719908Z 1 [Note] A temporary password is generated for root@localhost: <pidipNa0;g)

    现在用这个密码<pidipNa0;g)来连接一下看看:

    

    暂时无法执行命令,需要先重置root密码:

    

3.6 关闭MySQL

    命令: /usr/local/mysql/bin/mysqladmin shutdown -u root -p

    

3.7 其他

    3.7.1 查看MySQL从什么位置加载my.cnf配置文件

        

mysql的源码包如何打开 mysql5.7源码编译安装_MySQL

        如果存在多个文件,MySQL会以读取到的最后一个配置文件参数为准启动进程。

    3.7.2 数据库与数据库实例

        数据库是文件的集合,是依照某种数据模型组织起来并存放于二级存储器中的数据集合。

        数据库实例是程序,是位于用户与操作系统之间的一层数据管理软件,用户对数据库数据的任何操作,包括数据库定义、数据查询、数据维护、数据库运行控制等都是在数据库实例下进行的,应用程序只有通过数据库实例才能和数据库打交道。

    3.7.3 数据库的存储引擎是基于表的,而不是数据库!

    3.7.4 N/A.