开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第4天,点击查看活动详情
1、下载mysql安装包到要安装的目录下,我的目录为/tools/module
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz
2、解压并重命名
- 解压:
tar xvJf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz - 重命名:
mv mysql-8.0.20-linux-glibc2.12-x86_64 mysql-8.0 cd mysql-8.0mkdir data
3、分别创建用户组以及用户和密码
groupadd mysqluseradd -g mysql mysql
4、授权刚刚新建的用户
chown -R mysql.mysql /tools/module/mysql-8.0chmod 750 /tools/module/mysql-8.0/data -R
5、配置环境,
- 编辑:
vim /etc/profile - 添加到文件最后:
export PATH=$PATH:/tools/module/mysql-8.0/bin :wq保存退出后执行刷新配置文件:source /etc/profile
6、编辑my.cnf文件,注意其中的 /tools/module/mysql-8.0 路径
default-character-set=utf8mb4
[client]
#port=3306
socket=/var/lib/mysql/mysql.sock
[mysqld]
#port=3306
#server-id=3306
user=mysql
general_log = 1
general_log_file= /var/log/mysql/mysql.log
socket=/var/lib/mysql/mysql.sock
basedir=/tools/module/mysql-8.0
datadir=/tools/module/mysql-8.0/data
log-bin=/tools/module/mysql-8.0/data/mysql-bin
innodb_data_home_dir=/tools/module/mysql-8.0/data
innodb_log_group_home_dir=/tools/module/mysql-8.0/data/
character-set-server=utf8mb4
lower_case_table_names=1
autocommit=1
default_authentication_plugin=mysql_native_password
symbolic-links=0
# Disabling symbolic-links is recommended to prevent assorted security risks
# 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=/tools/module/mysql-8.0/data/mysql.log
pid-file=/tools/module/mysql-8.0/data/mysql.pid
#
# include all files from the config directory
!includedir /etc/my.cnf.d
7、初始化基础信息,得到数据库的初始密码
切换回/tools/module/mysql-8.0/bin路径下,执行:
./mysqld --user=mysql --basedir=/tools/module/mysql-8.0 --datadir=/tools/module/mysql-8.0/data/ --initialize
初始密码记得进行保存,下面会用到
8、添加mysqld服务到系统
切换到/tools/module/mysql-8.0 路径下,执行:
cp -a ./support-files/mysql.server /etc/init.d/mysqlcp -a ./support-files/mysql.server /etc/init.d/mysqld
9、赋予权限
chown 777 /etc/my.cnfchmod +x /etc/init.d/mysqlchmod +x /etc/init.d/mysqld
10、检查一下/var/lib/mysql是否存在,否则进行创建
mkdir /var/lib/mysqlchown -R mysql:mysql /var/lib/mysql/
11、启动服务,出现SUCCESS 说明启动成功
service mysql start
12、使用7中得到的初始密码进行登陆
mysql登录命令:mysql -uroot -p,输入密码回车进入数据库
如果不记得密码可以配置文件中设置跳过密码验证:
- 在 vim /etc/my.cnf文件中,在[mysqld]的段中加上一句:skip-grant-tables --跳过密码验证
- 然后保存退出,重启MySQL服务
- 然后输入mysql -uroot -p 敲两下回车,进入mysql
13、修改root密码
- 登录以后执行进入数据库:
USE mysql; - 执行修改密码:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码'; - 执行刷新权限:
flush privileges; - 用
exit;退出,并用新密码登录:mysql -uroot -p
14、修改远程连接并生效
create user 'root'@'%' identified by 'root对应的密码';grant all privileges on *.* to 'root'@'%' with grant option;flush privileges;
15、放开规则并连接测试
- 查看3306端口是否开放:
firewall-cmd --query-port=3306/tcp - 如果没有开发则添加:
firewall-cmd --zone=public --add-port=3306/tcp --permanent - 添加完成刷新防火墙:
firewall-cmd --reload
- 同时云服务安全组中需要配置实例访问规则,增加3306端口号
- 使用navicat连接测试