一、安装mysql5.7
-
先卸载原有的mysql,如果未安装,则跳过这一部分(以下内容均需要root权限执行)
# 1.查看有关 mysql 的包 dpkg --list|grep mysql # 2.卸载 mysql-common apt-get remove mysql-common # 3.卸载 mysql-server-8.0(可以根据自己的版本去进行卸载) apt-get autoremove --purge mysql-server-8.0 # 4.清除残留数据 dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P # 5.再次查看 mysql 的相关包 dpkg --list|grep mysql # 6.如果未打印任何内容,则卸载干净,如果打印其他,直接卸载即可 apt-get autoremove --purge mysql-apt-config # 7.删除mysql相关文件夹 rm -rf /etc/mysql /var/lib/mysql
-
安装mysql5.7
2.1. ubuntu下默认源的mysql版本是8.0.36# 1. 更新源 apt-get update # 2.查看mysql版本 apt-cache policy mysql-server
2.2. 更换源# 1. 先将原有源进行备份,以便于完成后修复 cp /etc/apt/sources.list /etc/apt/sources.list.back # 2. 编辑 /etc/apt/sources.list 文件,删除该文件中的所有内容,将以下内容增加到该文件中。 ============================================= # 阿里源 deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse ============================================= # 3. 更新镜像源 gpg --keyserver keyserver.ubuntu.com --recv 3B4FE6ACC0B21F32 gpg --export --armor 3B4FE6ACC0B21F32 | apt-key add - apt-get update # 4. 查看ubuntu下的mysql版本(此时已经可以看到mysql的版本为 5.7) apt-cache policy mysql-server
2.3. 安装mysql数据库# 1.安装命令 apt-get -y install mysql-server-5.7 # 2.查看是否安装成功 dpkg -l | grep mysql
2.4 设置开机自启动systemctl enable mysql
2.5 查看mysql版本
mysql -V
2.6 查看mysql运行状态
systemctl status mysql
2.7 直接 shell 中进入 mysql# 1.此时无密码即可登录 mysql # 2.使用 mysql 数据库 use mysql; # 3.修改root密码 alter user root@'localhost' identified with mysql_native_password by "1987890321"; # 4.重新加载授权表 flush privileges;
2.8 Mysql允许局域网或外部网络访问
# 1.找到/etc/mysql/my.cnf文件,如果有 [mysqld] ,在 [mysqld] 下增加 bind_address=0.0.0.0 # 2.如果没有 [mysqld],增加以下内容 [mysqld] bind_address=0.0.0.0 # 3.配置用户权限,登录 mysql,查询root用户允许访问的 host select host from user where user='root'; # 4.设置root用户允许访问的 host 为全部 update user set host = '%' where user ='root'; # 5.重新加载授权表 flush privileges;
2.9 重启mysql服务
systemctl restart mysql
2.10 将 源 还原回去
mv sources.list sources.list.old mv sources.list.back sources.list apt-get update
2.11 至此,mysql5.7 已经全部安装完成!
二、修改默认的 mysql 存储路径(这个我踩了一上午的坑!)(全部使用root权限进行操作)
- 首先将 mysql 服务停止
systemctl stop mysql
- 修改配置文件中的路径
vim /etc/mysql/my.cnf # 在 [mysqld] 下增加 datadir 参数(datadir 修改为你自己想要修改的路径) datadir=/home/zyp/mnt/ssd_4T/mysql
- 将 mysql 数据拷贝到目标路径下
# 3.1 mysql 数据的默认存储路径是 /var/lib/mysql cp -r /var/lib/mysql/* /home/zyp/mnt/ssd_4T/mysqlR # 3.2 修改目标文件夹的用户及用户组 chown -R mysql:mysql /home/zyp/mnt/ssd_4T/mysql # 3.3 同时将目标文件夹设为777权限 chown -R 777 /home/zyp/mnt/ssd_4T/mysql
- 你以为此时就大功告成了?当你此时启动数据库的时候 《systemctl start mysql》,他会出现这么一个错误
查看 mysql 的状态是这样的
查看日志信息(/var/log/mysql/error.log),他会这样显示
这里显示的错误是 ibdata1 问价没有权限写入,可之前不是已经设置了 目标路径下的所有文件权限为777最高权限了吗?
- 解决方案
# 1.删除数据库下的 ib_logfile0、ib_logfile1 文件 rm -rf ib_logfile0 ib_logfile1 # 2.修改 /etc/mysql/mysql.conf.d/mysqld.cnf 文件,将 datadir 修改为你的目标数据库路径 vim /etc/mysql/mysql.conf.d/mysqld.cnf datadir = /home/zyp/mnt/ssd_4T/mysql # 3.修改启动文件 vim /etc/apparmor.d/usr.sbin.mysqld 将以下路径替换为你的目标路径 修改前: /var/lib/mysql/ r, /var/lib/mysql/** rwk, 修改后:(注意!!!!这里mysql后一定要加"/",一定!!!!还有 行尾记得加 ",") /home/zyp/mnt/ssd_4T/mysql/ r, /home/zyp/mnt/ssd_4T/mysql/** rwk, # 4.重启 apparmor 服务 和 mysql 服务 systemctl restart apparmor systemctl start mysql
- 这时你会发现,mysql 正常了,脚也不疼了,吃饭也香了!