LNMP之mysql数据库

1.创建mysql用户
[root@web01 ~]# useradd -s /sbin/nologin mysql
[root@web01 ~]# id mysql
uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

2.下载mysql二进制软件包,提前配置好yum源,下载wget命令
[root@web01 ~]# yum install wget -y
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.re
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@web01 ~]# mkdir -p /home/mysql/tools
[root@web01 ~]# cd /home/mysql/tools/
# 该mysql文件600M左右,下载时间看网速
[root@web01 tools]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

 

[二进制安装mysql]
1.解压并且移动mysql二进制软件包路径
[root@web01 tools]# tar -zvxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@web01 tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /opt/mysql-5.7.26

2.生成软连接
[root@web01 opt]# ln -s /opt/mysql-5.7.26/ /opt/mysql
[root@web01 opt]# ls -l /opt/
总用量 0
lrwxrwxrwx 1 root root 18 3月 18 10:15 mysql -> /opt/mysql-5.7.26/
drwxr-xr-x 9 root root 129 3月 18 10:13 mysql-5.7.26

3.卸载centos7自带的mariadb库,防止冲突
[root@web01 mysql]# rpm -e --nodeps mariadb-libs


4.手动创建mysql配置文件 vim /etc/my.cnf
[root@web01 mysql]# cat /etc/my.cnf
[mysqld]
basedir=/opt/mysql/
datadir=/opt/mysql/data
socket=/tmp/mysql.sock
server_id=1
port=3306
log_error=/opt/mysql/data/mysql_err.log

[mysql]
socket=/tmp/mysql.sock

 


【初始化mysql服务端数据文件】
1.卸载系统自带的centos7 mariadb-libs,且安装mysql的依赖环境
rpm -qa mariadb-libs #检查是否存在
[root@web01 mysql]# yum install libaio-devel -y

2.创建mysql数据文件夹且授权
[root@web01 mysql]# mkdir -p /opt/mysql/data
[root@web01 mysql]# chown -R mysql.mysql /opt/mysql/

3.初始化数据库
[root@web01 mysql]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/

# 参数解释
--user=mysql 指定用户
--basedir 指定mysql安装目录
--datadir=/opt/mysql/data 指定数据文件夹
--initialize-insecure 关闭mysql安全策略
--initialize 开启mysql安全模式

 


【配置mysql客户端】
1.配置mysql启动脚本,定义mysqld.service,脚本如下
[root@web01 mysql]# cat /etc/systemd/system/mysqld.service
[Unit]
Description=MySQL server by chaoge
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=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000

 

【启动mysql数据库】
[root@web01 mysql]# systemctl start mysqld
[root@web01 mysql]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
[root@web01 mysql]# systemctl status mysqld
● mysqld.service - MySQL server by chaoge
Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-03-18 11:30:01 EDT; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 5315 (mysqld)
CGroup: /system.slice/mysqld.service
└─5315 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

3月 18 11:30:01 web01 systemd[1]: Started MySQL server by chaoge.
3月 18 11:30:01 web01 systemd[1]: Starting MySQL server by chaoge...

检查mysql启动状态
[root@web01 mysql]# netstat -tunlp|grep mysql
tcp6 0 0 :::3306 :::* LISTEN 5315/mysqld

[root@web01 mysql]# ps -ef|grep mysql |grep -v grep
mysql 5315 1 0 11:30 ? 00:00:00 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

 

【配置mysql命令的环境变量】
[root@web01 mysql]# echo "export PATH=/opt/mysql/bin:$PATH" >> /etc/profile
[root@web01 mysql]# source /etc/profile
[root@web01 mysql]# echo $PATH
/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

【登录mysql】
默认无需密码直接登录mysql,且身份是root
[root@web01 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.02 sec)

mysql> select user(); # 查看当前登录的用户
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

# 查看mysql所有用户信息
mysql> select user,authentication_string,host from mysql.user;
+---------------+-------------------------------------------+-----------+
| user | authentication_string | host |
+---------------+-------------------------------------------+-----------+
| root | | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
+---------------+-------------------------------------------+-----------+
3 rows in set (0.01 sec)

 

【给mysql设置登录密码加大安全性】

[root@web01 ~]# mysqladmin -uroot password 'chaoge666'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

# 再次登录,输入密码
[root@web01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26 MySQL Community Server (GPL)

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

posted @ 2020-08-18 11:07  王子建  阅读(296)  评论(0编辑  收藏  举报