MySQL的源码安装及基本部署(基于RHEL7.9)

这里源码安装mysql的5.7.44版本

一、源码安装

1.下载并解压mysql , 进入目录:

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.44.tar.gz
tar xf mysql-boost-5.7.44.tar.gz
cd mysql-5.7.44/

2.准备好mysql编译安装依赖:

yum install cmake gcc-c++ openssl-devel ncurses-devel rpcgen  -y
yum install  libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm #需到第三方仓库去找找

  可以用阿里云的链接下载https://mirrors.aliyun.com/centos-vault/7.7.1908/os/x86_64/Packages/libtirpc-devel-0.2.4-0.16.el7.x86_64.rpm​​​​​​​

3.添加参数选项:

cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_EXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_unicode_ci -DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0

   选项的详细解释如下:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #指定安装路径
-DMYSQL_DATADIR=/data/mysql \ #指定数据目录
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \ #指定套接字文件
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #指定启用INNODB存储引擎,默认用myisam
-DWITH_EXTRA_CHARSETS=all \ #扩展字符集
-DDEFAULT_CHARSET=utf8mb4 \ #指定默认字符集
-DDEFAULT_COLLATION=utf8mb4_unicode_ci \ #指定默认校验字符集
-DWITH_BOOST=/root/mysql-5.7.44/boost/boost_1_59_0/ #指定c++库依赖

4.开始编译安装:

尽量使用-j指定多线程进行编译以加快速度,但是要注意物理CPU数量以及内存大小,以及留有一定磁盘空间来安装mysql 一般(2-15min) 

make -j16 && make install

如编译过程出错、或者中断.Cmake 工具可以继续编译,得益于CMakeCache.txt的缓存记录.

如果想重新编译改变参数的话,需要将CMakeCache.txt先删除掉再进行编译,否则会采用之前的记录继续.

查看下大小
[root@mysql_node1 ~]# du -sh /usr/local/mysql/
1.9G        /usr/local/mysql/

二、Mysql的部署

在编译完成后即可进行mysql的部署

不能提前装有mysql类似软件!!! ⚠️  如果安装前有mysql环境一定要清理干净,再进行安装.

按照以下步骤依次操作即可:

#编译完成后:
#拷贝mysql启动程序文件 到 启动脚本目录
[root@mysql_node1 ~]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld

#添加mysql的bin目录环境变量
[root@mysql_node1 ~]# vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin
[root@mysql_node1 ~]# source ~/.bash_profile

#编辑mysql配置文件
[root@mysql_node1 ~]# cat /etc/my.cnf
[mysqld]
datadir=/data/mysql               #mysql的数据目录
socket=/data/mysql/mysql.sock     #mysql的套接字文件路径
symbolic-links=0                  #禁用mysql创建或者使用符号链接

#创建数据目录并授权给mysql用户 如果mysql用户不存在则创建一个( useradd -s /sbin/nologin  -M mysql   )
[root@mysql_node1 ~]# mkdir -p  /data/mysql
[root@mysql_node1 ~]# chown -R mysql.mysql /data/mysql/


#以mysql用户身份初始化mysql数据库 
[root@mysql_node1 ~]# mysqld --initialize --user=mysql      
2024-08-22T13:29:13.656433Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-08-22T13:29:13.763601Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-08-22T13:29:13.786517Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-08-22T13:29:13.849416Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 85cf7691-608a-11ef-a7c1-001c4294cf16.
2024-08-22T13:29:13.851008Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-08-22T13:29:13.981298Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T13:29:13.981313Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-08-22T13:29:13.981701Z 0 [Warning] CA certificate ca.pem is self signed.
2024-08-22T13:29:13.996329Z 1 [Note] A temporary password is generated for root@localhost: h!y_tT)pu4hx     #生成的随机临时密码, 只能用一次,即失效需要立即重新设置root账户密码才OK.  

#启动mysqld服务
[root@mysql_node1 ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/mysql_node1.err'.
 SUCCESS!
 
#使用chkconfig 工具使mysqld服务开机启动
[root@mysql_node1 ~]# chkconfig mysqld on

#进行mysql安全初始化
[root@mysql_node1 ~]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: #这里输入刚刚的临时密码

The existing password for the user account root has expired. Please set a new password.

New password:  #设置root密码

Re-enter new password:

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?                  #加载mysql密码安全插件,会使得密码必须复杂,试验环境一般不加.

Press y|Y for Yes, any other key for No: n
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n      #不改变root密码,因为刚刚已经设置了.

 ... skipping.
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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y         #是否现在重载表的权限,是
Success.

All done!

#登录测试mysql
[root@mysql_node1 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.44 Source distribution

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql启动 关闭 重启 

/etc/init.d/mysqld start stop restart

数据目录的含义

[root@mysql_node1 ~]# ll /data/mysql/

auto.cnf: MySQL 的自动配置文件,通常用于存储一些自动生成的配置参数。
ca-key.pem, ca.pem: 用于创建和管理用于 SSL 加密连接的证书颁发机构 (CA) 的密钥和证书文件。
client-cert.pem, client-key.pem: 用于客户端连接到 MySQL 服务器时的客户端 SSL 证书和密钥文件(如果启用)。
ib_buffer_pool: 用于缓存 InnoDB 存储引擎经常访问的数据的 InnoDB 缓冲池文件。
ibdata1: InnoDB 存储引擎的数据文件,其中存储了数据库表中的数据。
ib_logfile0, ib_logfile1: InnoDB 存储引擎的 InnoDB redo log 文件,用于记录事务的更改,以实现崩溃恢复。
ibtmp1: InnoDB 存储引擎的临时文件。
mysql/: 子目录,包含 MySQL 服务器本身的一些数据文件,例如权限信息和日志文件。
mysql_node1.err: MySQL 服务器的错误日志文件。
mysql_node1.pid: MySQL 服务器的进程 ID 文件。
mysql.sock: 用于本地进程之间通信的 MySQL 套接字文件。
mysql.sock.lock: 用于锁定 mysql.sock 文件的锁定文件。
performance_schema/: 子目录,包含用于性能分析的性能架构数据文件。
private_key.pem: 用于服务器端 SSL 加密连接的服务器私钥文件。
public_key.pem: 用于服务器端 SSL 加密连接的服务器公钥文件。
server-cert.pem: 用于服务器端 SSL 加密连接的服务器证书文件。
server-key.pem: 用于服务器端 SSL 加密连接的服务器私钥文件。
sys/: 子目录,包含一些内部系统文件。

以上步骤完成,即可正常使用mysql了👍🎉

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值