Ubuntu 安装 mysql

本篇文章持续更新,记录遇到mysql安装坑

1. 卸载mysql

sudo rm /var/lib/mysql/ -R
sudo rm /etc/mysql/ -R
sudo apt-get autoremove mysql* --purge
sudo apt-get remove apparmor

2. 安装

2.1、方法1

sudo apt update
// 安装mysql5.7
sudo apt install mysql-server -y

2.2、方法2

dev.mysql.com/downloads/repo/apt/ 复制下载链接

// 下载
curl -OL https://dev.mysql.com/get/mysql-apt-config_0.8.12-1_all.deb
// 用dpkg 安装
sudo dpkg -i mysql-apt-config*
// 更新你的库
sudo apt update
// 用第一种方法安装
sudo apt install mysql-server -y

3. 验证安装状态

// 查看服务启动状态
sudo systemctl status mysql.service
// 开启mysql 服务
sudo systemctl start mysql.service

4. 关闭服务并卸载

sudo systemctl stop mysql.service && sudo systemctl disable mysql.service
# 确保你备份了你的数据库,以防你之后想使用它们。你可以通过运行下列命令卸载 MySQL:
sudo apt purge mysql*
# 清理依赖:
sudo apt autoremove

5. 解决mysql不用密码就能登录的问题

5.1 查看是否有空记录(网上找的就是坑,user不可能为空)

select count(*) from user where user=””;

5.2 有就删除不管有没有都

delete from user where user=””;
flush privileges; (必须的)

5.3 执行以下命令(我他吗也不知道是干嘛的)

update user set plugin = “mysql_native_password”;

5.4 执行以下代码

use mysql;
update user set authentication_string=password("你的密码") where user='root'; //(无password字段的版本,也就是版本<=5.7的) 
update user set password=password('你的密码') where user='root'; //(有password字段的版本,版本>5.7的) 
update user set plugin="mysql_native_password"; 
flush privileges; 
exit;

6.远程访问

6.1 防火墙

// 查看防火墙
// 关闭防火墙开启防火墙
//1.查看防火墙状态
systemctl status firewalld
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
//2.查看防火墙是否开机启动
systemctl is-enabled firewalld
//3.关闭防火墙
systemctl stop firewalld
systemctl stop firewalld.service
systemctl status firewalld
//4.禁用防火墙(系统启动时不启动防火墙服务)
systemctl disable firewalld
systemctl disable firewalld.service
systemctl is-enabled firewalld
// 开放3306端口
/sbin/iptables -I INPUT -p tcp –dport 3306-j ACCEPT

// ubuntu 防火墙
//https://www.linuxidc.com/Linux/2016-12/138259.htm
sudo ufw status

6.2.设置远程密码

// 设置远程密码
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345678';
// 查看密码策略
SHOW VARIABLES LIKE 'validate_password%';
修改密码策略:
set global validate_password_policy=LOW;
set global validate_password_length=6;
ALTER USER 'root'@'localhost' IDENTIFIED BY '12345678';
// 跟以下是等价的
update user set authentication_string=password("12345678") where user = "root";
flush privileges;

7.0 修改阿里云的安全组规则

安全组-安全组规则-配置安全组,将mysql 端口3306加入进去就行了。
以上全部内容,有新问题随时更新

8.0 其它问题

如果用navicate 还是不能连接 10061 ,则找到 /etc/mysql/mysql.conf.d/mysqld.cnf
找到配置

#bind-address        = 127.0.0.1
// 改为
bind-address        = 0.0.0.0

重启mysql service mysql restart 或者 systemctl restart mysql.service

本作品采用《CC 协议》,转载必须注明作者和本文链接
编程两年半,喜欢ctrl(唱、跳、rap、篮球)
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!