源码安装 MySQL 数据库,有这份教程就够了
Posted 煜学志
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码安装 MySQL 数据库,有这份教程就够了相关的知识,希望对你有一定的参考价值。
获取更多精彩
准备
安装依赖
1[root@localhost Downloads]# yum install cmake ncurses ncurses-devel gcc gcc-devel gcc-c++
2...
删除 my.cnf 文件,如果有
1[root@localhost Downloads]# rm -rf /etc/my.cnf
创建用户
1[root@localhost Downloads]# useradd -s /sbin/nologin mysql
下载源码包
1[root@localhost Downloads]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.25.tar.gz
解压
1[root@localhost Downloads]# ls -l
2-rw-rw-r--. 1 leo leo 49107578 7月 8 19:48 mysql-boost-5.7.25.tar.gz
3[root@localhost Downloads]# tar -zxvf mysql-boost-5.7.25.tar.gz
4...
5[root@localhost Downloads]# ls -l
6drwxr-xr-x. 36 7161 31415 4096 12月 21 2018 mysql-5.7.25
7-rw-rw-r--. 1 leo leo 49107578 7月 8 19:48 mysql-boost-5.7.25.tar.gz
编译安装
cmake
配置项
1-DCMAKE_INSTALL_PREFIX=/opt/mysql/5.7.25 # 安装目录 --basedir
2-DMYSQL_DATADIR=/opt/mysql/5.7.25/data # 数据目录 --datadir
3-DSYSCONFDIR=/opt/mysql/5.7.25/etc # my.cnf配置文件目录 --defaults-file
4-DMYSQL_TCP_PORT=3306 # 端口号 --port
5-DSYSTEMD_PID_DIR=/opt/mysql/5.7.25/mysqld.pid # 当MySQL由systemd管理时,在其中创建PID文件的目录
6-DMYSQL_UNIX_ADDR=/opt/mysql/5.7.25/mysql.sock # 服务器在其上侦听套接字连接的Unix套接字文件路径 --socket
7-DDEFAULT_CHARSET=utf8 # 服务器字符集 --character_set_server
8-DDEFAULT_COLLATION=utf8_general_ci # 服务器排序规则 --collation_server
9-DWITH_EXTRA_CHARSETS=all # 包括的额外字符集
10-DWITH_INNOBASE_STORAGE_ENGINE=1
11-DWITH_BLACKHOLE_STORAGE_ENGINE=1
12-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
13-DWITH_ZLIB=bundled # 使用压缩库支持构建
14-DWITH_SSL=bundled # 支持加密连接
15-DENABLED_LOCAL_INFILE=1 # 控制本地MySQL客户端库的内置默认功能
16-DWITH_EMBEDDED_SERVER=0 # 是否构建libmysqld嵌入式服务器库,MySQL 5.7.17开始不推荐使用嵌入式服务器库,并且已在MySQL 8.0中将其删除
17-DENABLE_DOWNLOADS=1 # 是否下载可选文件
18-DWITH_DEBUG=0 # 是否支持调试
19-DWITH_BOOST=./boost/boost_1_59_0 # 指定Boost库目录位
20-DWITH_SYSTEMD=1 # 是否启用安装systemd支持文件
cmake
配置
1[root@localhost Downloads]# cd mysql-5.7.25
2[root@localhost mysql-5.7.25]# mkdir bld && cd bld
3[root@localhost bld]# cmake ../ \
4-DCMAKE_INSTALL_PREFIX=/opt/mysql/5.7.25 \
5-DMYSQL_DATADIR=/opt/mysql/5.7.25/data \
6-DSYSCONFDIR=/opt/mysql/5.7.25/etc \
7-DMYSQL_TCP_PORT=3306 \
8-DSYSTEMD_PID_DIR=/opt/mysql/5.7.25/mysqld.pid \
9-DMYSQL_UNIX_ADDR=/opt/mysql/5.7.25/mysql.sock \
10-DDEFAULT_CHARSET=utf8 \
11-DDEFAULT_COLLATION=utf8_general_ci \
12-DWITH_EXTRA_CHARSETS=all \
13-DWITH_INNOBASE_STORAGE_ENGINE=1 \
14-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
15-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
16-DWITH_ZLIB=bundled \
17-DWITH_SSL=bundled \
18-DENABLED_LOCAL_INFILE=1 \
19-DWITH_EMBEDDED_SERVER=0 \
20-DENABLE_DOWNLOADS=1 \
21-DWITH_DEBUG=0 \
22-DWITH_BOOST=../boost/boost_1_59_0 \
23-DWITH_SYSTEMD=1
24...
25-- Configuring done
26-- Generating done
27-- Build files have been written to: /home/xxx/Downloads/mysql-5.7.25/bld
28[root@localhost bld]# make && make install
29...
配置文件 my.cnf
1[root@localhost bld]# cd /opt/mysql/5.7.25/
2[root@localhost 5.7.25]# mkdir etc
3[root@localhost 5.7.25]# vim etc/my.cnf
my.cnf
1[mysqld]
2port=3306
3basedir=/opt/mysql/5.7.25
4datadir=/opt/mysql/5.7.25/data
5socket=/opt/mysql/5.7.25/mysql.sock
6log-error=/opt/mysql/5.7.25/log/mysqld.log
7pid-file=/opt/mysql/5.7.25/mysqld.pid
8symbolic-links=0
9max_allowed_packet=20M
10default-time-zone = '+8:00'
11bind_address='0.0.0.0'
12character_set_server=utf8
13collation_server=utf8_general_ci
14autocommit=1
15max_connections=1024
创建 log
目录
1[root@localhost 5.7.25]# mkdir log
更改 安装目录的所属用户为 mysql
1[root@localhost 5.7.25]# chown -R mysql:mysql /opt/mysql/5.7.25/
初始化数据目录
1[root@localhost 5.7.25]# bin/mysqld --initialize --user=mysql
22020-07-08T14:00:35.171027Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
32020-07-08T14:00:35.523723Z 0 [Warning] InnoDB: New log files created, LSN=45790
42020-07-08T14:00:35.596471Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
52020-07-08T14:00:35.662833Z 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: 6559e620-c123-11ea-aa1a-009b27e04c84.
62020-07-08T14:00:35.665818Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
72020-07-08T14:00:35.667859Z 1 [Note] A temporary password is generated for root@localhost: R*udj_rdi3K&
启动数据库
一般启动方法
1[root@localhost 5.7.25]# bin/mysqld --daemonize --user=mysql
systemd
管理
1[root@localhost 5.7.25]# cp usr/lib/systemd/system/mysqld.service usr/lib/systemd/system/mysqld@.service /usr/lib/systemd/system/
2[root@localhost 5.7.25]# vim /usr/lib/systemd/system/mysqld.service
修改 mysqld.service
1#
2# systemd service file for MySQL forking server
3#
4
5[Unit]
6Description=MySQL Server
7Documentation=man:mysqld(8)
8Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
9After=network.target
10After=syslog.target
11
12[Install]
13WantedBy=multi-user.target
14
15[Service]
16User=mysql
17Group=mysql
18
19Type=forking
20
21PIDFile=/opt/mysql/5.7.25/data/mysqld.pid
22
23# Disable service start and stop timeout logic of systemd for mysqld service.
24TimeoutSec=0
25
26# Execute pre and post scripts as root
27PermissionsStartOnly=true
28
29# Needed to create system tables
30ExecStartPre=/opt/mysql/5.7.25/bin/mysqld_pre_systemd
31
32# Start main service
33ExecStart=/opt/mysql/5.7.25/bin/mysqld --daemonize --pid-file=/opt/mysql/5.7.25/data/mysqld.pid $MYSQLD_OPTS
34
35# Use this to switch malloc implementation
36EnvironmentFile=-/etc/sysconfig/mysql
37
38# Sets open_files_limit
39LimitNOFILE = 5000
40
41Restart=on-failure
42
43RestartPreventExitStatus=1
44
45PrivateTmp=false
重载服务配置
1[root@localhost 5.7.25]# systemctl daemon-reload
开机自动启动
1[root@localhost 5.7.25]# systemctl enable mysqld
启动服务
1[root@localhost 5.7.25]# systemctl start mysqld
登录数据库修改密码
1[root@localhost 5.7.25]# bin/mysql -u root -p
2Enter password:
3Welcome to the MySQL monitor. Commands end with ; or \g.
4Your MySQL connection id is 2
5Server version: 5.7.25
6
7Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
8
9Oracle is a registered trademark of Oracle Corporation and/or its
10affiliates. Other names may be trademarks of their respective
11owners.
12
13Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
修改密码
1mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'ghJKJB&^!';
2Query OK, 0 rows affected (0.00 sec)
忘记密码
1# my.cnf 加入 skip-grant-tables ,启动
2
3mysql>use mysql
4mysql>update user set authentication_string = password('ghJKJB&^!') where user='root';
5
6# my.cnf 去掉 skip-grant-tables ,重启
7
8mysql>ALTER USER 'root'@'localhost' IDENTIFIED BY 'ghJKJB&^!';
9Query OK, 0 rows affected (0.00 sec)
查看数据库
1mysql> show databases;
2+--------------------+
3| Database |
4+--------------------+
5| information_schema |
6| mysql |
7| performance_schema |
8| sys |
9+--------------------+
104 rows in set (0.00 sec)
11
12mysql>
以修改后的密码登录
1[root@localhost 5.7.25]# bin/mysql -u root -p'ghJKJB&^!' # 密码用引号引起来,-p后面不加空格
至此,mysql
安装完成。
配置环境变量 /etc/profile
1export PATH=$PATH:/opt/mysql/5.7.25/bin
登录数据库
1[root@localhost 5.7.25]# mysql -h 127.0.0.1 -P 3306 -u root -p'ghJKJB&^!'
END
扫描二维码
关注“煜学志”,获取更多精彩
个人网站:http://www.vleity.com
以上是关于源码安装 MySQL 数据库,有这份教程就够了的主要内容,如果未能解决你的问题,请参考以下文章