一 下载软件 www.mysql.com
二 安装mysql
mkdir -p /app
cd app
2.1
tar xf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz #解压文件
2.2
mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql #改文件夹的名字为mysql
2.3
ln -s mysql mysql-5.7.28-linux-glibc2.12-x86_64 #创建软连接方便以后升级
2.4
vim /etc/profile
添加 export PATH=/app/mysql/bin:$PATH
source /etc/profile##生效环境变量
2.5 添加mysql用户
[root@localhost ~]# grep mysql /etc/group #查询系统中是否有mysql这个用户组,没有则添加。
[root@localhost ~]# groupadd mysql#增加一个名为 Mysql的用户组
[root@localhost ~]# grep mysql /etc/passwd#查询系统中是否有mysql这个用户,没有则添加。
[root@localhost ~]# useradd mysql -g mysql -M -s /sbin/nologin #增加一个名为CentOS Mysql的用户。
2.6创建数据目录
mkdir /data/mysql -p
2.7赋予目录权限
chown -R mysql.mysql /app/*
chown -R mysql.mysql /data/*
或 进入 mysql目录
chown -R mysql:mysql ./
进入data目录
chown -R mysql:mysql ./
2.8 初始化数据库
初始化数据,初始化管理员的临时密码
mysqld --initialize --user=mysql --basedir=/app/mysql --datadir=/data/mysql
(报错原因:在/data/mysql 存在文件 解决: \rm -rf /data/mysql/*
报错原因: Linux系统中缺少libaio-devel 软件包 解决: yum install -y libaio-devel)
方法2:
初始化数据,初始化管理员的密码为空
\rm -rf /data/mysql/* ###先删掉数据
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/app/mysql --datadir=/data/mysql
yum remove mariadb* #######卸载软件
netstat -lnp|grep 330
pkill mysqld
2.9 写配置文件
vim /etc/my.cfg
[mysqld]
user=mysql
basedir=/app/mysql
datadir=data/mysql
server_id=6
port=3306
socket=tmp/mysql.sock
[mysql]
socket=/tmp/mysql.sock
prompt=3306[\\d]>
2.10 查看mysql是否启动 的三种方式
2.10.1
[root@mysql7-13 support-files]# netstat -lnp|grep 330
tcp6 0 0 :::3306 :::* LISTEN 17109/mysqld
2.10.2
[root@mysql7-13 support-files]# ss -lnp|grep 330
tcp LISTEN 0 80 :::3306 :::*
2.10.3
[root@mysql7-13 support-files]# ps -ef|grep mysqld
root 16943 1 0 21:45 pts/1 00:00:00 /bin/sh /app/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/mysql7-13.host.com.pid
mysql 17109 16943 0 21:45 pts/1 00:00:01 /app/mysql/bin/mysqld --basedir=/app/mysql --datadir=/data/mysql --plugin-dir=/app/mysql/lib/plugin --user=mysql --log-error=mysql7-13.host.com.err --pid-file=/data/mysql/mysql7-13.host.com.pid --socket=/tmp/mysql.sock --port=3306
root 17185 16897 0 21:58 pts/1 00:00:00 grep --color=auto mysqld
2.11 拷贝文件 自动启动
方法一
cp mysql.server /etc/inti.d/mysqld
验证
service mysqld start ##启动服务
service mysqld status ##查看状态
service mysqld stop ##停止服务
方法二
vim /etc/systemd/system/mysqld.service
[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=/app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
验证:一
[root@mysql7-13 //]# systemctl start mysqld
[root@mysql7-13 //]# netstat -lnp|grep 330
tcp6 0 0 :::3306 :::* LISTEN 17353/mysqld
验证二
[root@mysql7-13 //]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2020-01-25 22:24:39 CST; 8min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 17353 (mysqld)
CGroup: /system.slice/mysqld.service
└─17353 /app/mysql/bin/mysqld --defaults-file=/etc/my.cnf
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.137913Z 0 [Warning] CA certificate ca.pem is self signed.
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.137971Z 0 [Note] Skipping generation of RSA key pair as key ...ctory.
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.138074Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.138587Z 0 [Note] IPv6 is available.
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.138619Z 0 [Note] - '::' resolves to '::';
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.138639Z 0 [Note] Server socket created on IP: '::'.
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.142247Z 0 [Note] Failed to start slave threads for channel ''
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.147430Z 0 [Note] Event Scheduler: Loaded 0 events
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: 2020-01-25T14:24:40.147944Z 0 [Note] /app/mysql/bin/mysqld: ready for connections.
1月 25 22:24:40 mysql7-13.host.com mysqld[17353]: Version: '5.7.28' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)
Hint: Some lines were ellipsized, use -l to show in full.
2.12 设置管理员密码
[root@mysql7-13 //]# myslqadmin -uroot -p password 123
再登录
mysql -uroot -p 回车
2.13 查看用户基本信息
select user,authentication_string,host from mysql.user;
desc mysql.user;
用户权限运用回顾
create user oldboy@'10.0.0.%'identified by '123'
dorp user oldboy@'10.0.0.%'
alter user oldboy@'10.0.0.%'identified by '456'
select user,host from mysql.user;
grant all on *.* to oldboy@'10.0.0.%'
revoke drop,delete on *.* from oldboy@'10.0.0.%'
show grants for oldboy@'10.0.0.%'
本地管理员用户密码忘记.
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '123456';
[root@db01 ~]# pkill mysqld
[root@db01 ~]# systemctl start mysqld