1.下载mysql压缩包 mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz,放在 /usr/local/ 文件夹下。
2.解压 tar -xvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz ,此时/usr/local/ 文件夹下有个mysql-5.7.27-linux-glibc2.12-x86_64,将其改名为mysql。( )
3.在 /usr/local/mysql 文件夹下,新建data文件夹。
mkdir data
4.新建mysql的用户组
groupadd mysql
useradd -r -g mysql mysql
5.将安装目录所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
6.初始化数据库
/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
若出现错误如下:
2020-02-14 16:35:45 [ERROR] Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
2020-02-14 16:35:45 [ERROR] Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
则执行 /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
若出现如下错误:
/usr/local/mysql/bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
则执行 yum install -y libaio
再执行 /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
若出现如下错误:
[ERROR] --initialize specified but the data directory has files in it. Aborting.
则将/usr/local/mysql/data文件夹中数据全部删除
再执行 /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize
执行成功后,末尾有个临时密码,如下:
2020-02-14T08:42:11.833558Z 1 [Note] A temporary password is generated for root@localhost: uc#s9pGgV&dJ
若未出现错误,只有警告,末尾也没有临时密码出现,如下,则继续下一步 ,在配置中设置跳过密码,设置密码后再关闭跳过密码设置
7.完成初始化后,编辑 vi /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
user=mysql
port=12306
character-set-server=utf8
# sql_mode设置
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 最大允许传输包的大小
max_allowed_packet=1000M
#跳过密码验证,忘记密码 可以设置,然后修改密码,再关闭
#skip-grant-tables
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
# include all files from the config directory
!includedir /etc/my.cnf.d
9.将mysql加入到服务 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
10.设置为开机自启动 chkconfig mysql on
11.配置环境变量 vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
保存退出,执行source /etc/profile
12.启动数据库
/usr/local/mysql/support-files/mysql.server start
13.改数据库密码 输入 mysql -u root -p ,会让输入密码,密码为第6步中最后的初始密码:uc#s9pGgV&dJ,输入之后即进入数据库,
若出现错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
则在 vi /etc/my.cnf 在此文件中加上以下语句
[client]
socket=/tmp/mysql.sock
保存,重启mysql service mysqld restart
use mysql;
set password=password("你的密码");
若出现错误:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
则使用 update user set authentication_string=password('你的密码') where user='root'; 【5.7之后版本】
ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码'; 【其他版本】
update mysql.user set password=password('你的密码') where user='root'; 【5.6版本】
flush privileges; //生效修改的配置
exit; //退出
14.设置远程连接
mysql -u root -p 再输入密码进入数据库
use mysql;
update user set host='%' where user = 'root';
flush privileges;
exit;
15.设置关闭防火墙,开机禁用防火墙
关闭防火墙 systemctl stop firewalld
开机禁用防火墙 systemctl disable firewalld
启动mysql service mysql start
重启mysql service mysqld restart
重启mysql /etc/init.d/mysql restart
创建数据库:
CREATE DATABASE IF NOT EXISTS calluna_mall DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;
新建 testuser 用户,并给这个用户授权 test_db 这个库的权限:【%表示本地和远程连接都可以,localhost表示仅允许本地连接】
grant all privileges on test_db.* to 'testuser'@'%' identified by 'pwd123456' with grant option;
不新建用户,给 testuser 用户授权 test_db 这个库的权限:【%表示本地和远程连接都可以,localhost表示仅允许本地连接】
grant all on test_db.* to 'testuser'@'%';
给 testuser 用户取消 test_db 这个库的权限
revoke all on test_db.* from 'testuser'@'%';
flush privileges;