centos7.6从零开始编译nginx+mysql+php(三) 安装Mysql
in 云服务器 with 0 comment

centos7.6从零开始编译nginx+mysql+php(三) 安装Mysql

in 云服务器 with 0 comment

上一篇文章讲述了nginx的搭建,这篇来讲mysql

mysql的下载地址推荐使用网易的镜像站 http://mirrors.163.com/

5.7.24的下载地址是http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz

安装步骤

# 先回到软件下载目录
[root@localhost nginx-1.14.0]# cd /software

# 安装依赖组件
[root@localhost software]# yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++

# 创建用户组、用户
[root@localhost software]# groupadd mysql
[root@localhost software]# useradd -s /sbin/nologin -g mysql mysql -M
[root@localhost software]# id mysql

# 下载mysql包
[root@localhost software]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-boost-5.7.24.tar.gz

# 解压mysql源码包
[root@localhost software]# tar zxvf mysql-boost-5.7.24.tar.gz

# 进入解压后的目录,对mysql进行配置(5.5以上都是cmake)
[root@localhost software]# cd mysql-5.7.24/
[root@localhost mysql-5.7.24]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DSYSCONFDIR=/usr/local/mysql/etc -DSYSTEMD_PID_DIR=/usr/local/mysql -DDEFAULT_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=boost -DWITH_SYSTEMD=1

# 编译和安装
[root@localhost mysql-5.7.24]# make && make install

# 初始化数据库及启动
[root@localhost mysql-5.7.24]# chown -R mysql.mysql /usr/local/mysql/
[root@localhost mysql-5.7.24]# cd /usr/local/mysql/

[root@localhost mysql-5.7.24]# cat >> my.cnf << EOF
[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
default-character-set=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M 

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
EOF

[root@localhost mysql]# chown mysql.mysql my.cnf
[root@localhost mysql]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@localhost mysql]# echo 'export PATH' >> /etc/profile
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
[root@localhost mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@localhost mysql]# systemctl daemon-reload 
[root@localhost mysql]# systemctl start mysqld
[root@localhost data]# ps -ef|grep mysql

# 设置mysql开机启动
[root@localhost mysql]# systemctl enable mysqld

# 查看Mysql启动状态
[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2019-05-06 23:02:54 CST; 16s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 28056 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─28056 /usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/lo...

5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.266299Z 0 [Note]...l
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.267420Z 0 [Note]...4
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.270692Z 0 [Note]....
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.270934Z 0 [Warni....
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.273422Z 0 [Note]...6
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.273491Z 0 [Note]....
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.273503Z 0 [Note]...;
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.273518Z 0 [Note]....
5月 06 23:02:54 centos7 mysqld[28053]: 2019-05-06T15:02:54.279956Z 0 [Note]...s
5月 06 23:02:54 centos7 systemd[1]: Started MySQL Server.
Hint: Some lines were ellipsized, use -l to show in full.

安装完成后我们尝试使用mysql命令测试连接数据库,此时会报2002的错误。

[root@localhost mysql]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/usr/local/mysql/mysql.sock' (2)

一开始我百度都是说我安装mysql的过程中出现了问题,推荐我重新安装或者使用TCP/IP的方式去链接数据库。

后来根据这篇文章,找到了解决办法。启动mysql错误解决方案,学会查看错误日志:mysql.sock丢失,mysqld_safe启动报错

我们可以通过ln软连接mysql.sock发送到/usr/local/mysql/mysql.sock位置,操作如下。

# 首先找到mysql.sock的位置
[root@localhost mysql]# find / -name mysql.sock
/var/lib/mysql/mysql.sock

# 然后软连接到/usr/local/mysql/mysql.sock
[root@localhost mysql]# ln -s /var/lib/mysql/mysql.sock /usr/local/mysql/mysql.sock

# 再次测试mysql命令
[root@localhost mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

这次错误从2002变成了1045,原因是数据库账号密码不正确。

测试环境里我们可以设置跳过密码验证来解决这个问题

# 编辑mysql配置文件
[root@localhost mysql]# vi /etc/my.cnf
# 在[mysqld]下方加入一行 skip-grant-tables

批注 2019-05-06 232016.png

保存后,重启mysql服务

[root@localhost mysql]# systemctl restart mysqld

# 再次输入mysql命令,访问正常
[root@localhost mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.24 Source distribution

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

到此MYSQL编译成功,下一篇更新PHP。

参考文档

CentOS 7下编译安装Nginx+MySQL+PHP
启动mysql错误解决方案,学会查看错误日志:mysql.sock丢失,mysqld_safe启动报错
重置密码解决MySQL for Linux错误 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

Comments are closed.