centos7部署MySQL 5.7

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7部署MySQL 5.7相关的知识,希望对你有一定的参考价值。

一、部署安装mysql

注:在安装MySQL前需要卸载掉当前已有的mariadb,然后需要安装gcc环境
1、安装cmake

  [root@mysql ~]# wget https://github.com/Kitware/CMake/archive/v3.16.4.tar.gz                # 下载cmake
[root@mysql ~]# yum -y install openssl-devel          # 有时候安装cmake的时候会报错,一般就是openssl这个依赖包没有装
[root@mysql ~]# tar zxf cmake-3.16.4.tar.gz 
[root@mysql ~]# cd cmake-3.16.4/
[root@mysql cmake-3.16.4]# ./bootstrap 
[root@mysql cmake-3.16.4]# gmake && gmake install 

2、安装ncurses

[root@mysql ~]# wget ftp://ftp.gnu.org/gnu/ncurses/ncurses-5.9.tar.gz
[root@mysql cmake-3.16.4]# cd ..
[root@mysql ~]# tar zxf ncurses-5.9.tar.gz 
[root@mysql ~]# cd ncurses-5.9/
[root@mysql ncurses-5.9]# ./configure && make && make install 

3、安装bison

[root@mysql ~]# wget http://ftp.gnu.org/gnu/bison/bison-3.1.tar.gz
[root@mysql ~]# tar zxf bison-3.1.tar.gz 
[root@mysql ~]# cd bison-3.1/
[root@mysql bison-3.1]# ./configure && make && make install 

4、安装boost

[root@mysql ~]# wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz/download
[root@mysql ~]# tar zxf boost_1_59_0.tar.gz 
[root@mysql ~]# mv boost_1_59_0/ /usr/local/boost

5、创建MySQL用户及所需目录

[root@mysql ~]# useradd -r -M -s /sbin/nologin mysql 
[root@mysql ~]# mkdir -p /usr/local/mysql/data

6、安装及初始化MySQL

[root@mysql ~]# tar zxf mysql-5.7.29.tar.gz 
[root@mysql ~]# cd mysql-5.7.29/
[root@mysql mysql-5.7.29]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_SYSTEMD=1 -DWITH_BOOST=/usr/local/boost && make && make install#配置环境变量
[root@mysql mysql-5.7.29]# echo ‘export PATH=$PATH:/usr/local/mysql/bin‘ >> /etc/profile
[root@mysql mysql-5.7.29]# source /etc/profile#更改属主属组
[root@mysql mysql-5.7.29]# cd /usr/local/
[root@mysql local]# chown -R mysql:mysql mysql/#进行初始化
[root@mysql ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

当执行初始化操作后,在返回信息的最后一行,将显示着MySQL数据库root用户的密码,也有可能不显示,那就说明root用户没有密码,我这里显示的密码如下(此密码建议先复制到一个文本上,一会修改密码需要用到,如果此密码丢失,需要重新初始化MySQL,重新初始化目录的步骤:删除/usr/local/mysql/data目录下的所有文件及目录,然后才可以重新初始化成功):
技术图片
7、配置并启动MySQL

#再次修改当前目录下的属组及属主(初始化后,产生的一些文件)
[root@mysql ~]# chown -R mysql:mysql /usr/local/mysql/#编写MySQL的主配置文件[root@mysql ~]# vim /etc/my.cnf[mysqld]basedir=/usr/local/mysqldatadir=/usr/local/mysql/dataport=3306server_id=1            
#当架构中存在多个MySQL服务器,那么这个server_id就是各个MySQL数据库的唯一性。socket=/usr/local/mysql/mysql.socklog-error=/usr/local/mysql/data/mysqld.errsql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION#复制其服务脚本并启动MySQL
[root@mysql ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
[root@mysql ~]# systemctl start mysqld 
执行启动命令时,会报错。。。。。。

技术图片
可以通过MySQL错误日志,定位问题所在:

[root@mysql mysql]# tail data/mysqld.err 2020-04-10T16:41:01.982743Z 0 [Note] 
InnoDB: Buffer pool(s) load completed at 200411  0:41:012020-04-10T16:41:01.985619Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.2020-04-10T16:41:01.985630Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.2020-04-10T16:41:01.986055Z 0 [Warning] CA certificate ca.pem is self signed.2020-04-10T16:41:01.986086Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.2020-04-10T16:41:01.986148Z 0 [Note] Server hostname (bind-address): ‘*‘; port: 33062020-04-10T16:41:01.986174Z 0 [Note] IPv6 is available.2020-04-10T16:41:01.986180Z 0 [Note]   - ‘::‘ resolves to ‘::‘;2020-04-10T16:41:01.986195Z 0 [Note] Server socket created on IP: ‘::‘.2020-04-10T16:41:01.987181Z 0 [ERROR] Can‘t start server: can‘t check PID filepath: No such file or directory

解决办法:

修改其服务控制脚本中指定的PID路径即可:
[root@mysql mysql]# vim /usr/lib/systemd/system/mysqld.service 
PIDFile=/usr/local/mysql/mysqld.pid        # 修改这两行
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/mysqld.pid $MYSQLD_OPTS

确定MySQL数据库启动成功

[root@mysql ~]# systemctl daemon-reload          # 重载配置文件
[root@mysql ~]# systemctl restart mysqld         # 重启服务
[root@mysql ~]# netstat -anput | grep 3306       # 确认服务启动
tcp6       0      0 :::3306                 :::*                    LISTEN      92944/mysqld  

8、更改MySQL的root登录密码

[root@mysql ~]# mysqladmin -u root -p password ‘123.com‘
Enter password:            #这里输入初始化MySQL后返回的密码#执行成功后,会输出一些warning类的提示信息,无关紧要的,是提示直接将新密码暴露在了终端,不安全。
[root@mysql ~]# mysql -u root -p123.com       # 使用新密码登录

以上是关于centos7部署MySQL 5.7的主要内容,如果未能解决你的问题,请参考以下文章

Mysql部署

Mysql部署

Centos7编辑安装mysql(mysql-5.7)

CentOS 7 部署MySQL 5.7(主从复制,传统方式及GTID)

Mysql搭建

Centos7中编译安装MySQL(mysql-5.7)