MySQL 编译安装全流程
MySQL 编译安装全流程
0x1 安装相关依赖
- cmake最新版MySQL的编译工具
sudo yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel
sudo yum install zlib zlib-devel cmake ncurses ncurses-devel bison bison-devel
如下的几个依赖在CentOS7中需要安装,CentOS6不需要
sudo yum install perl perl-devel autoconf
- 安装boost(根据版本可选)
如果安装的MySQL5.7及以上的版本,在编译安装之前需要安装boost,因为高版本mysql需要boots库的安装才可以正常运行。否则会报CMake Error at cmake/boost.cmake:81
错误
- 切换到
/usr/local
目录,然后在这个目录下下载boost
MySQL5.7.24要求boost的版本是1.59,更高版本的不适用MySQL5.7.24
wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
- 解压并改名
tar zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 boost
- 在预编译安装MySQL时要加上
-DWITH_BOOST=/usr/local/boost
0x2 获取MySQL源码包
![image-20201126100750718](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126100750.png)
- 获取预安装MySQL源码包下载地址
如图,在第①步选择MySQL版本,第②步选择源码包下载,第③步选择最后一个下载按钮,获得下载地址https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.49.tar.gz
- 下载
cd
wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.49.tar.gz
如果报错未安装wget,则执行
yum install -y wget
- 解压缩
tar zxvf mysql-5.6.49.tar.gz
之后就可以在当前目录看到解压缩后的MySQL源码包,如图
![image-20201126101434321](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126101434.png)
- 进入源码包查看
cd mysql-5.6.49
ls -lh
如图
![image-20201126101612632](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126101612.png)
0x3 创建MySQL专用用户
- 创建操作系统层的 MySQL 专用账户和用户组,均命名为 mysql:
groupadd mysql
useradd -g mysql mysql
- 设置用户操作系统资源的限制,使用 vi 编辑器打开 limits 文件,执行命令如下:
vi /etc/ecurity/limits.conf
- 在文件的最后增加下列内容:
mysql soft nproc 2047
mysql hard nproc 16384
mysql soft nofile 1024
mysql hard nofile 65536
- 保存退出
0x4 编译安装
- 执行命令:
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=l \
-DWITH_FEDERATED_STORAGE_ENGINE=l \
-DWITH_BLACKHOLE_STORAGE_ENGINE=l \
-DWITH_EXAMPLE_STORAGE_ENGINE=l \
-DWITH_PARTITION_STORAGE_ENGINE=l \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=l \
-DWITH_READLINE=ON \
-DSYSCONFDIR=/data/mysqldata/3306 \
-DMYSQL_UNIX_ADDR=/data/mysqldata/3306/mysql.sock
cmake 命令执行成功的话,则最后输出信息类似:
-- Configuring done
-- Generating done
- Build files have been written to : /data/software/mysql-5.6.49
如果编译过程中出现错误,或者参数变更要重新配置,可以通过 m 命令,删除源码包目录下的 Cmakecache. txt 文件,而后重新执行 cmake 命令,或者干脆将 mysql 源码目录删除,再重新解压缩并进行编译配置。
接下来执行编译和安装,这一步骤依赖机器性能,可能耗时较长:
make && make install
0x5 赋予权限与配置环境变量
如果前面操作没有碰到错误的话,源码编译方式安装 MySQL 就成功了 。接下来还要对编译好的 MySQL 软件做些初始化工作,以便我们能够更方便地调用,比如授予目录权限、修改环境变量等。
- 首先修改 MySQL 软件所在目录的拥有者为 mysql 用户,执行命令如下:
chown -R mysql:mysql /usr/local/mysql
- 修改mysql用户环境变量:
vi /home/mysql/.bash_profile
![image-20201126102725258](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126102725.png)
- 加入内容:
PATH=/usr/local/mysql/bin:$PATH
- 保存退出
0x6 创建数据库服务
接下来咱们先为将要创建的 MySQL 数据库做准备性工作,比如创建文件目录、修改所有者等。如果您的环境是按照前面章节中,源码编译的内容一步一步跟着操作的话,接下来最好也保持跟进 , 最起码保持文件路径相同,能节省些脑细胞:如果您不是源码编译的,好吧!也可以,不过我建议您最好还是使用源码编译的环境,后面真的能节省不少脑细胞。
考虑到我们前面编译安装MySQL 时,指定的数据文件默认存储路径为/data/mysqldata/3306,因此这里我们就要将相应的目录都创建好,执行操作如下:
- 创建目录
mkdir -p /data/mysqldata/{3306/{data,tmp,binlog},backup,scripts}
cd /data
chown -R mysql:mysql mysqldata
su - mysql
cd /usr/local/mysql
- 配置参数文件
vi /data/mysqldata/3306/my.cnf
编写如下内容:
[client]
port = 3306
socket = /data/mysqldata/3306/mysql.sock
# The MySQL server
[mysqld]
port = 3306
user = mysql
socket = /data/mysqldata/3306/mysql.sock
pid-file = /data/mysqldata/3306/mysql.pid
basedir = /usr/local/mysql
datadir = /data/mysqldata/3306/data
tmpdir = /data/mysqldata/3306/tmp
open_files_limit = 10240
explicit_defaults_for_timestamp
sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
# Buffer
max_allowed_packet = 256M
max_heap_table_size = 256M
net_buffer_length = 8K
sort_buffer_size = 2M
join_buffer_size = 4M
read_buffer_size = 2M
read_rnd_buffer_size = 16M
# Log
log-bin = /data/mysqldata/3306/binlog/mysql-bin
binlog_cache_size = 32m
max_binlog_cache_size = 512m
max_binlog_size = 512m
binlog_format = mixed
log_output = FILE
log-error = ./mysql-error.log
slow_query_log = on
slow_query_log_file = ../slow_query.log
general_log = 0
general_log_file = ../general_query.log
expire-logs-days = 14
# lnnoDB
innodb_data_file_path = ibdata1:2048M:autoextend
innodb_log_file_size = 256M
innodb_log_files_in_group = 3
innodb_buffer_pool_size = 1024M
[mysql]
no-auto-rehash
prompt = (\u@\h) [\d] )>\_
default-character-set = utf8mb4
- 初始化数据库
/usr/local/mysql/scripts/mysql_install_db --datadir=/data/mysqldata/3306/data --basedir=/usr/local/mysql
- 判断是否成功
如果出现类似如下内容则成功
Installing MySQL system tables ... OK
Filling help tables .. . OK
To sLart mysqld at boot time you have to copy
support-files/mysql. server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/ usr/ local/mysql/bin/ mysqladmin - u root password ’ new-password'
/ usr/local/mysql/ bin/ mysqladmin -u root h localhost. localdomain password ’ new-password'
Alternatively you can run:
/ usr/local/mysql/bin/ mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default.
This is
st 「ongly recommended for production servers.
See Lhe manual for more instructions.
You can start the MySQL daemon with.
cd . , / usr/ local/mysql/bin/ mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql『test ; perl mysql- test-run. pl
Please report any problems with the ./bin/ mysqlbug script!
The latest information about MySQL is available on the web at
http: //www. mysql. com
Support MySQL by buying support/licenses at http: //shop.mysql.com
New default config file was created as /usr/local/mysql/my. cnf and
wi 11 be used by default by the server when you start it.
You may edit this file to change server settings
0x7 启动数据库服务
- 启动MySQL服务
mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
0x8 配置MySQL数据库
- 进入mysql
mysql
- 删除其他用户
DELETE FROM mysql.user WHERE (user,host) NOT IN (SELECT 'root','localhost');
- 修改root用户名和密码
UPDATE mysql.user SET user='yourname',password=PASSWORD('yourpassword');
yourname, yourpassword自行填写
- 删除测试数据库
TRUNCATE table mysql.db;
- 使所做操作生效
FLUSH privileges;
- 退出mysql
exit
0x9 MySQL 服务管理配置
- 创建管理脚本
- 创建中间定义文件,目的提高脚本复用性:
vi /data/mysqldata/scripts/mysql_env.ini
- 添加以下内容
# set env
MYSQL_USER=system
MYSQL_PASS='Wwseyy'
# check parameter
if [ $# -ne 1 ]
then
HOST_PORT=3306
else
HOST_PORT=$1
fi
- 创建启动脚本
vi /data/mysqldata/scripts/mysql_db_startup.sh
- 添加以下内容
#!/bin/sh
# Created by chenjunjie
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySQL Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysqldata/${HOST_PORT}/my.cnf &
- 创建关闭脚本
vi /data/mysqldata/scripts/mysql_db_shutdown.sh
- 添加以下内容
#!/bin/sh
# Created by chenjunjie
source /data/mysqldata/scripts/mysql_env.ini
echo "Startup MySQL Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock shutdown
- 创建mysql登录脚本
vi /data/mysqldata/scripts/mysqlplus.sh
- 添加如下内容
#!/bin/sh
# Created by chenjunjie
source /data/mysqldata/scripts/mysql_env.ini
echo "Login MySQL Service: localhost_"${HOST_PORT}
/usr/local/mysql/bin/mysql -u${MYSQL_USER} -p${MYSQL_PASS} -S /data/mysqldata/${HOST_PORT}/mysql.sock $2
- 赋予执行权限
chmod 700 /data/mysqldata/scripts/*.sh
- 配置环境变量
vi ~/.bash_profile
添加如下内容
PATH=/data/mysqldata/scripts:$PATH
![image-20201126105606775](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126105606.png)
0x10 配置开机自启动
- 编辑/etc/re.local 文件,需要在 root 用户下执行:
vi /etc/rc.local
添加如下内容
sudo -i -u mysql /data/mysqldata/scriptes/mysql_db_startup.sh 3306 > /home/mysql/mysql_db_startup.log 2>&1
![image-20201126105830559](https://raw.githubusercontent.com/CoderChen01/typora-images/master/20201126105830.png)
0x11 大功告成
一切都己筹备就绪 ,准备好迎接我们的 MySQL 数据库之旅吧!