zl程序教程

您现在的位置是:首页 >  IT要闻

当前栏目

linxu编译安装MySQL

2023-02-18 16:34:36 时间
/configure和cmake
  • 使用cmake进行编译安装的时候,源码文件夹可以多次使用。因为cmake在创建编译的过程中是独立于源码进行编译的的。

  • cmake是跨平台的,支持不同平台的编译。

  • 使用configure的话,一个源码文件夹只能编译一次,如果需要多次编译就需要将编译过的源码文件夹删除重新解压源码包进行编译。

流程:
源文件 --> cmakelist --> cmake --> makefile --> make --> 可执行文件

image

./configure -->makefile -->make --->可执行文件

链接:https://www.freesion.com/article/8963110/

源码安装mysql(以为mysql5.6.51为例)

1.安装相关依赖包:
[root@CentOS8 ~]# yum -y install gcc gcc-c++ cmake make bison  zlib-devel \
libcurl-devel  boost-devel   ncurses-devel gnutls-devel libxml2-devel \
openssl-devel libevent-devel libaio-devel perl-Data-Dumper perl perl-devel
2.创建对应的用户和数据目录
[root@CentOS8 ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql

[root@CentOS8 ~]# mkdir   /data/mysql

[root@CentOS8 ~]# chown mysql.mysql /data/mysql
3.下载并解压缩源码包

https://downloads.mysql.com/archives/community/

image

  • 二进制包的命名方式:带linux字样,说明已经被编译成适合linux的二进制包了

  • 源码包:命名不带操作系统的版本信息。

[root@CentOS8 ~]# tar xvf mysql-5.6.51.tar.gz  -C /usr/local/src
4. 源码编译安装 MySQL

查看每个编译选项的作用:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html

[root@CentOS8 ~]# cd mysql-5.6.51/
[root@CentOS8 mysql-5.6.51]# cmake . \
-DCMAKE_INSTALL_PREFIX=/apps/mysql \  --- 安装mysql这个程序的路径
-DMYSQL_DATADIR=/data/mysql/ \   ---- mysql的数据存放位置
-DSYSCONFDIR=/etc/ \  --- 配置文件位置
-DMYSQL_USER=mysql \ --- 用户账号
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

[root@CentOS8 mysql-5.6.51]#  make -j 8 && make install

#如果出错,执行rm -f CMakeCache.txt然后重来
5. 准备环境变量
[root@CentOS8 ~]# echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

[root@CentOS8 ~]# . /etc/profile.d/mysql.sh
6.生成数据库文件
  • 进入mysql的程序存放位置,里面有一个脚本可以帮我们初始化生成数据库(/apps/mysql/scripts/mysql_install_db)

  • 执行这个脚本的时候会依赖别的脚本(/apps/mysql/bin/my_print_defaults),所以运行这个脚本的时候不能进到这个脚本所在的路径下面(/apps/mysql/scripts),因为这样就找不到相对于当前目录下的这个脚本。所以执行这个脚本的时候要在/app/mysql/这个路径下执行。

[root@CentOS8 ~]# cd   /apps/mysql/

[root@CentOS8 mysql]# scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
注意:
7.准备配置文件

编译安装mysql后,/etc/my.cnf这个配置文件(编译完成后自己生成的)。这个文件是maridb自带的,所以不符合要求,需要将我们编译安装生成的配置文件拷贝一份为my.cnf

[root@CentOS8 ~]# cp -b /apps/mysql/support-files/my-default.cnf /etc/my.cnf

8.准备启动脚本,并启动服务
[root@CentOS8 ~]# cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@CentOS8 ~]# chkconfig --add mysqld

[root@CentOS8 ~]# service mysqld start
9.测试
[root@CentOS8 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>