如何编译安装MySQL数据库

文章目录

  • 环境
  • 准备工作
  • 编译安装过程

环境

一台centos 7.6 虚拟机

准备工作

把mysql的压缩包拖到xshell中

  • boost_1_59_0.tar.gz
  • mysql-5.7.17.tar.gz

编译安装过程

【1】安装环境依赖包

yum install -y \
ncurses \
ncurses-devel \
bison \
cmake

【2】创建用户

useradd -s /sbin/nologin mysql   

【3】解压缩并放到指定目录中

tar zxvf mysql-5.7.17.tar.gz -C /opt/
tar zxvf boost_1_59_0.tar.gz -C /usr/local/

【4】路径优化

cd /usr/local
mv boost_1_59_0 boost

【5】安装软件依赖包

cd /opt/mysql-5.7.17/
yum install gcc gcc-c++ -y

[root@localhost mysql-5.7.20]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \         '指定安装路径'
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \     '连接数据库'
-DSYSCONFDIR=/etc \                               '配置文件路径'
-DSYSTEMD_PID_DIR=/usr/local/mysql \                 'PID文件指定路径'
-DDFAULT_CHARSET=utf8 \                           '字符集'
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \              '存储引擎'
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \             '数据存放位置'
-DWITH_BOOST=/usr/local/boost \                      'c++库'
-DWITH_SYSTEMD=1                        '开启必要进程'

------------注意:如果在CMAKE的过程中有报错,当报错解决后,需要把源码目录中的CMakeCache.txt文件删除,然后再重新Cmake

【6】编译安装

make&&make install

【7】给mysql工作目录赋予权限

chown -R mysql.mysql /usr/local/mysql/ 

【8】调整配置文件

[root@localhost opt]# vim /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

【9】将配置声明到环境变量中去

echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile

【10】source执行文件

source /etc/profile

【11】初始化数据库

[root@localhost opt]# cd /usr/local/mysql/

[root@localhost mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

【12】将service服务启动脚本放到systemd目录下执行

cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/

【13】重新加载、启动服务、查看端口状态

systemctl daemon-reload
systemctl start mysqld
netstat -ntap | grep 3306

【14】设置为开机自启动

systemctl enable mysqld

【15】设定密码

mysqladmin -u root -p password "19961207"

【16】登录数据库,输入密码

mysql -u root -p

【17】授权远程登录

mysql> grant all privileges on *.* to 'root'@'' identified by '19961207' with grant option;

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

你可能感兴趣的:(MySQL数据库,数据库,mysql,linux,centos,经验分享)