服务器安装mysql
本机Linux系统操作一样
清理mysql,卸载原有的mysql
# 查找mysql的软件包和依赖包
rpm -qa | grep mysql
# 使用命令删除查找出来的程序,没有查出来忽略这一步
yum remove mysql-XXX-XXX-...
# 删除mysql的配置文件,卸载不会自动删除,先使用命令找出来
find / -name mysql
# 使用命令依次删除
rm -rf [查出来的每一条进行复制粘贴]
安装mysql
# 在根目录的usr/local文件夹下创建mysql文件夹并进入
cd /usr/local
mkdir mysql
cd mysql
# 使用命令下载软件包rpm文件
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
# 安装yum repo文件并更新yum缓存
rpm -ivh mysql80-community-rekease-e17-1.noarch.rpm
# 更新yum命令
yum clean all
yum makecache
# 使用yum安装mysql
# 查看mysql版本
yum repolist all | grep mysql
# 安装yum-utils
yum -y install yum-utils
# 如果安装mysql8.XXX,就将mysql80-community改为启用状态
# 把mysql57-community改为禁用状态
# disable:禁用 enable:启用
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
# 安装mysql
yum -y install mysql-community-server
# 开启mysql服务
systemctl start mysqld.service
# 获取初始密码登录mysql(mysql安装之后会创建一个root@localhost账户,并把初始密码放到了/var/log/mysqld.log文件中)
cat /var/log/mysqld.log | grep password
# 使用初始密码登录mysql
mysql -u root -p #回车之后会提示输入密码,密码输入不显示
# 修改初始密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '自定义密码';
# 自定义的密码规则,长度为8,规则为必须含有大写,小写,符号和数字,详情需要百度mysql登录密码策略
# 忘记密码重置密码(ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)])
vim /etc/my.cnf # 没有vim的执行yum -y install vim命令下载
skip-grant-table # 在[mysqld]后面任意一行添加,跳过密码验证的部分,设置完密码记得删除
flush privileges # 刷新
Systemctl restart mysqld.service # 重启mysql服务,就可以免密登录了,然后进行修改密码
# 免密登录后先将密码置空再进行修改,否则报错
update user set authentication_string ='' where user ='root';
# 再执行sql语句
alter user 'root'@'localhost' identified with mysql_native_password by '你的密码';
# 添加用户并赋予用户权限(先添加用户再赋予权限)
create user 'root'@'%' identified by '密码';
# 授权
grant all privileges on *.* to 'root'@'%';
在防火墙中开启3306端口
# 查看所有打开的端口
firewall-cmd --zone=public --list-ports
# 开启3306端口 添加--permanent永久生效,不添加则暂时生效
firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --permanent --add-port=3306/tcp
# 移出开放的端口(这一步不用做)
firewall-cmd --permanent --remove-port=端口号/tcp
# 重启防火墙
firewall-cmd --reload
# 关于防火墙的命令
#systemctl status firewalld # 查看防火墙状态
#systemctl stop firewalld # 暂时关闭防火墙
#systemctl disable firewalld # 永久关闭防火墙
#systemctl enable firewalld # 重启防火墙
# 将mysql服务加入开机启动项,并启动mysql
systemctl enable mysqld.service
systemctl start mysqld.service
# 常见的mysql服务命令
#mysql -u username -p # 登录mysql
#quit # 退出mysql
#systemctl start mysqld.service # 启动mysql
#systemctl stop mysqld.service # 结束mysql
#systemctl restart mysqld.service # 重启
#systemctl enable mysqld.service # 开机自启
#select version(); # 查看mysql版本
开启远程mysql服务(外网Navicat连接MySql)
# 修改mysql数据库下user表中的host值(可能是你的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 "user" 表里的 "host" 项,从"localhost"改称"%"登录mysql数据库)
mysql -u root -p
use mysql;
update user set host='%' where user='root';
使用授权方式
# 赋予任何主机访问数据的权限(有时会报错,多执行)
flush privileges; # 不行百度原因
grant all privileges on *.* to 'root'@'%' with grant option;
# 从任何主机输入用户名和密码连接
grant all privileges on *.* to '指定用户名'@'%' identified by '指定密码' with grant option;
#指定用户连接主机和用户名密码连接
grant all privileges on *.* to '指定用户名'@'指定IP' identified by '指定密码' with grant option;