Skip to content

Cmake MySQL5.7

一. 安装说明

MySQL 5.7.x 源码安装,5.7 以上版本安装都信赖 boost 库

需要编译过程,如设备配置低,编译时间长并会有卡死现象,功能模块自定制

版本环境

二. 程序安装

程序安装前,先卸载默认的 mariadb

$ rm -rf /etc/my.cnf /etc/my.cnf.d/
$ rpm -qa | grep mariadb | xargs yum remove -y

源码安装教程可参考 官网,安装步骤:配置->编译->安装

2.1 基本环境

1)下载依赖包 & 程序包,根据需求版本进行下载

1
2
3
$ yum -y install ncurses-devel cmake libaio-devel openssl-devel gcc gcc-c++ bison
$ tar -xf mysql-boost-5.7.31.tar.gz
$ cd mysql-5.7.31

2)基于 CMake 进行配置

注:版本不一样,所需要的环境也不一样,可能还需要安装其它的依赖包,如 Perl 相关的包

$ cmake . \
-DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DMYSQL_DATADIR=/opt/mysql/data \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/opt/mysql/mysql.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_SSL=system \
-DWITH_BOOST=boost

选项说明:
-DCMAKE_INSTALL_PREFIX:安装路径
-DMYSQL_DATADIR:数据目录
-DMYSQL_TCP_PORT:端口号
-DMYSQL_UNIX_ADDR:套接字文件位置

选项说明:

  • -DCMAKE_INSTALL_PREFIX:安装路径
  • -DMYSQL_DATADIR:数据目录
  • -DMYSQL_TCP_PORT:端口号
  • -DMYSQL_UNIX_ADDR:套接字文件位置

3)编译安装,安装过程较长,可开启多线程操作

$ make -j2 && make install

PS:-j2:代表同时开启多个线程共同实现编译操作,如报错则取消多线路操作

2.2 数据初始化

1)创建一个数据库专用账号

1
2
3
$ useradd -r -s /sbin/nologin mysql
$ id mysql
uid=997(mysql) gid=993(mysql) groups=993(mysql)

2)修改目录权限并进入安装目录执行初始化操作

PS:注意最后输出的是默认密码,后期修改密码需要用上,先另存备用

1
2
3
4
5
$ chown -R mysql:mysql /opt/mysql
$ cd !$
$ bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
...
2023-11-28T02:43:31.295939Z 1 [Note] A temporary password is generated for root@localhost: .*JkCh<gq2p#

3)拷贝 mysql.server 脚本到 /etc/init.d 目录,然后启动数据库

1
2
3
4
$ cp support-files/mysql.server /etc/init.d/mysql
$ service mysql start
Starting MySQL.Logging to '/usr/local/mysql/data/Dev-Pc.err'.
 SUCCESS!

4)编写 MySQL 配置文件并重启服务

$ vim /etc/my.cnf
[client]
#default-character-set=utf8
socket=/opt/mysql/mysql.sock

[mysqld]
port=3306
basedir=/opt/mysql
datadir=/opt/mysql/data
socket=/opt/mysql/mysql.sock
character-set-server=utf8
lower_case_table_names=1

$ service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

三. 安全设置

3.1 修改密码

1)修改管理员密码

1
2
3
4
$ bin/mysqladmin -uroot password 'newpassword' -p
Enter password:.*JkCh<gq2p#
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

2)测试密码修改是否成功

$ bin/mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.31 Source distribution

Copyright (c) 2000, 2022, 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>

3)修改安全配置

$ bin/mysql_secure_installation
# 第一项修改管理员密码回车跳过外,其他都选y
Securing the MySQL server deployment.

Enter password for user root: 

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?

Press y|Y for Yes, any other key for No: 
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... 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
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
 - 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!

4)密码修改,将原旧密码/远进行修改(非必要)

1
2
3
4
5
$ mysql -u root -p
Enter password:
> set password=password('新密码');
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '新密码';
> flush privileges;

3.2 开机自启

1)添加服务至开机启动项并配置环境变量

1
2
3
4
5
6
7
8
$ chkconfig --add mysql
$ chkconfig mysql on

$ vim /etc/profile.d/mysql5731.sh
# MySQL
export MYSQL_HOME=/opt/mysql
export PATH=$PATH:$MYSQL_HOME/bin
$ source /etc/profile

3.3 开启远登

1)确认关闭或开启防火墙 mysql-3306 端口的外部访问

$ firewall-cmd --zone=public --add-port=3306/tcp --permanent
$ firewall-cmd --reload

参数说明:

  • --zone: 作用域,网络区域定义了网络连接的可信等级
  • --add-port: 添加端口与通信协议,格式为:端口/通讯协议,协议是tcp 或 udp
  • --permanent: 永久生效,没有此参数系统重启后端口访问失效

2)赋予指定用户远程权限,如 root

1
2
3
$ bin/mysql -uroot -p
mysql> GRANT ALL ON *.* TO root@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
mysql> flush privileges;