一、.参考博客
虚拟机中安装mysql 完整教程-简单实用-亲测有效( CentOS7 版本)_虚拟机安装mysql-CSDN博客
二、实践配置
1.检查
(1)
rpm -qa | grep -i mysql
rpm -qa | grep -i mariadb
有,则直接删除
rpm -e –nodeps mariadb-5.5.68-1.el7.x86_64
rpm -e –nodeps mariadb-libs-5.5.68-1.el7.x86_64
(2)
whereis mysql
有,则用命令 rm -rf 全部删了
2.下载
下载地址:
MySQL :: Download MySQL Community Server
3.进入/usr/local目录下,把下载的MySQL放到这个目录下(可用finalshell登录远程上传)
4.解压安装包
tar -xJvf mysql-8.0.36-linux-glibc2.17-x86_64.tar.xz
5.重命名文件夹
mv mysql-8.0.36-linux-glibc2.17-x86_64 mysql
6.进入mysql目录,在新建一个data目录
7.创建用户组以及用户和密码
groupadd mysql
useradd -g mysql mysql
8.授权用户
chown -R mysql.mysql /usr/local/mysql
9.再进入bin目录,初始化数据库
./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ --initialize --lower-case-table-names=1
10.初始化后有一个密码(后面登录mysql用到的密码)
11.编辑my.cnf文件,这里没有vim可以用vi
vim /etc/my.cnf
编辑内容如下:
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
character-set-server=UTF8MB4
lower_case_table_names = 1
# 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=/usr/local/mysql-8.0/logs/mysqld-error.log
# pid-file=/tmp/mysqld.pid
12.回退上一级目录,然后添加mysql服务到系统服务中
cd ..
cp -a ./support-files/mysql.server /etc/init.d/mysql
13.授权以及添加服务
chmod +x /etc/init.d/mysql
chkconfig --add mysql
14.启动mysql
service mysql start
15.添加软链接
ln -s /usr/local/mysql/bin/mysql /usr/bin
16.登录mysql (密码在步骤7)
mysql -u root -p
17.修改密码,这里我把密码设置成123456
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
18. 执行【flush privileges;】使密码立即生效
flush privileges;
19.查看mysql端口权限
show variables like '%port%';
20.修改远程配置权限,以便window的navicat远程登录
use mysql;
update user set host='%' where user='root';
flush privileges;
21.退出mysql(exit)
22.重启mysql
systemctl restart mysql
23.开启3306端口防火墙,以便window远程访问虚拟机IP
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-port
三、防火墙有关命令
查看防火墙的状态(systemctl status firewalld、firewall-cmd --state)
暂时关闭防火墙(systemctl stop firewalld)
永久关闭防火墙(systemctl disable firewalld)
开启防火墙(systemctl start firewalld)
开放指定的端口(firewall-cmd --zone=public --add-port=8080/tcp --permanent)
移除指定的端口(firewall-cmd --zone=public --remove-port=8080/tcp --permanent)
防火墙立即生效(firewall-cmd --reload)
查看开放的端口(firewall-cmd --zone=public --list-port)
四、mysql有关管理命令
(1)
启动:service mysql start
停止:service mysql stop
重启:service mysql restart
更新配置:service mysql reload
状态:service mysql status、ps -ef | grep mysql
(2) 注意service 命令和systemctl命令千万别混用,可能导致效果相反或syatemctl命令失效(开机自启通用)
-
启动 MySQL 服务
sudo systemctl start mysql
-
停止 MySQL 服务
sudo systemctl stop mysql
-
重启 MySQL 服务
sudo systemctl restart mysql
-
重新加载 MySQL 配置
sudo systemctl reload mysql
-
查看 MySQL 服务状态
sudo systemctl status mysql
-
启用 MySQL 服务开机自启
sudo systemctl enable mysql
-
禁用 MySQL 服务开机自启
sudo systemctl disable mysql