LNMP架构实现之Mysql的安装

说明:本片文章所安装mysql版本为5.7.32
需要其他版本的mysql可以前往163镜像源下载
http://mirrors.163.com/mysql/Downloads

1.安装Mysql

1.1 下载并安装mysql(二进制安装)

[root@localhost ~]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/\
mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz

[root@localhost ~]# tar xf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz -C \
/application/mysql-5.7.32

二进制安装,解压及安装完成

1.2 做版本管理

服务器上线使用mysql目录
开发人员使用mysql-版本号目录

[root@localhost ~]# ln -s /application/mysql-5.7.32 /application/mysql

1.3 下载libaio共享库

[root@localhost ~]# yum install libaio-devel -y 

1.4 卸载mariadb

mariadb与mysql功能相近,可能会导致冲突
千万不要用 yum remove 卸载,因为会同时自动卸载依赖

[root@localhost ~]# rpm -e --nodeps mariadb-libs

2.配置并初始化mysql

2.1 创建mysql管理用户

[root@localhost ~]# useradd -s mysql /sbin/nologin -M        
[root@localhost ~]# 重要,一定不要忘记创建管理用户

2.2 创建/etc/my.cnf

[mysqld]
basedir = /application/mysql/
datadir = /application/mysql/data
socket  = /tmp/mysql.sock
server_id = 1
port = 3306
log_error = /application/mysql/data/zhb_mysql.err

[mysql]
socket = /tmp/mysql.sock
prompt = "\\u@\\h : \\d\\r:\\m:\\s>"

2.3 创建data目录并设置权限

[root@localhost ~]# yum install libaio-devel -y   #易错点,所以写了两次
[root@localhost ~]# mkdir -p /application/data 
[root@localhost ~]# chown mysql.mysql /application/mysql -R

2.4 初始化数据库

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

3.启动mysql数据库

3.1 编写systemd管理脚本

[root@localhost ~]# vim /etc/systemd/system/mysqld.service

[Unit]
Description=MySQL Server by oldboy
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.targe

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

3.2 启动mysql数据库并检查

[root@localhost ~]# systemctl start mysqld          #完成3.1可使用此方法启动

[root@localhost ~]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/
mysqld.service to /etc/systemd/system/mysqld.service.

[root@localhost ~]# netstat -lntup | grep mysqld
tcp6       0      0 :::3306        :::*        LISTEN      1301/mysqld  

[root@localhost ~]# ps -ef | grep mysqld | grep -v grep
mysql     1301     1  0 02:44 ?     00:00:02 /application/mysql/bin/mysqld 
--defaults-file=/etc/my.cnf

3.3 mysql设置密码

#设置密码
[root@localhost ~]# mysqladmin -uroot password '000000'
#进入数据库
[root@localhost ~]# mysql -uroot -p000000
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.32 MySQL Community Server (GPL)

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

root@localhost : (none):21: >

3.4 为mysql命令设置环境变量

[root@localhost ~]# echo "export PATH=/application/mysql/bin:$PATH" >>/etc/profile\
[root@localhost ~]# . /etc/profile

如果对你有帮助,希望点赞

转载请标明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值