mysql安装步骤

一、安装前的一些基础优化

1、优化镜像源

[ ~]# rm -fr /etc/yum.repos.d/* curl -o /etc/yum.repos.d/CentOS-Base.repo  http://mirrors.aliyun.com/repo/Centos-7.repo
[ ~]# curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

2、安装一些会用到的软件包

[ ~]#  yum install net-tools vim tree htop iftop gcc gcc-c++ glibc iotop lrzsz sl wget unzip telnet nmap nc psmisc dos2unix bash-completion bash-completion-extra sysstat  libaio-devel -y

3、关闭防火墙,SELinux

到此安装完成
[ ~]# systemctl stop firewalld
[ ~]# sed -i ‘/^SELINUX=/c SELINUX=disabled‘ /etc/selinux/config

4、调整单个进程最大能打开文件的数量

[ ~]# echo ‘* - nofile 65535‘ >> /etc/security/limits.conf

二、安装mysql数据库

1、下载mysql安装包

[ ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz

2、移除系统自带的mariadb

[ ~]#  rpm -qa |grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[ ~]# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64

3、创建mysql用户

[ ~]#  useradd -s /sbin/nologin mysql

4、创键 /application 目录

[ ~]# mkdir /application

5.解压mysql压缩包到指定目录下

[ ~]# tar xf mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz -C /application/
  [ ~]# mv mysql-5.7.26-linux-glibc2.12-x86_64  /application/mysql  #重命名

6、设置环境变量

[ /application]# vim /etc/profile
#加入这条配置信息
export PATH=/application/mysql/bin:$PATH
[ /application]# source /etc/profile

7、创建 /data目录并授权

[ /application]# mkdir /data/mysq/data -p
[ /application]# chown -R mysql.mysql /data

8、初始化数据

[ /application]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data

说明:

--initialize 参数:

\1. 对于密码复杂度进行定制:12位,4种

\2. 密码过期时间:180

\3. 给用户设置临时密码

9、准备配置文件

[ ~]# cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF

10、启动MySQL

[ /application]# cd /etc/init.d/
[ /etc/init.d]# cp /application/mysql/support-files/mysql.server  /etc/init.d/mysqld 
[ /etc/init.d]# netstat  -lntp|grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      25760/mysqld        
[ /etc/init.d]# ps -ef|grep 3306
mysql    25760 25594  0 10:35 pts/0    00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/data/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=mysqlJX.err --pid-file=/data/mysql/data/mysqlJX.pid --socket=/tmp/mysql.sock --port=3306
root     25796 13111  0 10:37 pts/0    00:00:00 grep --color=auto 3306

11、用systemd管理mysql

cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target 
[Service]
User=mysql
Group=mysql
#这行以后根据生产环境的要求进行更改
ExecStart=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF

12、将mysql加入开机自启动

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

13、设置mysql root用户禁止远程登录

[ ~]# grant all on *.* to ‘localhost‘ identified by ‘123456‘;

14、设置远程登录用户

grant all on *.* to ‘wxw‘ identified by ‘123‘;

相关推荐