一、检查系统是否已安装其他版本mysql
打开系统终端, 执行如下命令,检查系统是否已安装mysql
ps -ef | grep mysql
出现红色mysql 即已安装mysql
若已安装mysql,最好把下面卸载清理命令都执行一遍:
sudo apt-get -y update
sudo apt-get -y autoremove --purge mysql-server
sudo apt-get -y remove mysql-server # 没用到,已经没有mysql-server
sudo apt-get -y autoremove mysql-server # 没用到,已经没有mysql-server
sudo apt-get -y remove mysql-common
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm -rf /etc/mysql/ /var/lib/mysql #删除Mysql数据文件夹
#清理残留数据
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
sudo apt -y autoremove
sudo apt -y autoclean
sudo apt-get -y remove mysql-client-5.7
sudo apt-get -y remove mysql-server-5.7
sudo apt-get -y remove mysql-client-core-5.7
sudo apt-get -y autoremove
sudo apt-get -y --purge remove && sudo apt-get -y autoclean
sudo apt-get -f -y install
sudo dpkg-reconfigure -a
sudo dpkg --configure -a
sudo apt-get -y update
sudo apt-get -y upgrade
二、重新安装mysql8.o.x
方式一:在线安装,执行如下命令,
sudo apt-get update
sudo apt-get install mysql-server
方式二:离线安装,需要提前下载好需要的安装包
我个人在用的脚本:
#!/bin/bash
echo -e "正在执行离线安装mysql-8.0"
sudo apt-get -f -y install
#apt-get install libaio1
dpkg -i libaio1_0.3.112-9ubuntu1_amd64.deb
#apt-get install libmecab2
dpkg -i libmecab2_0.996-14build3_amd64.deb
sudo apt-get -f -y install
dpkg -i mysql-community-client-plugins_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-client-core_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-common_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-client_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i libmysqlclient21_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i libmysqlclient-dev_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-client_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-server-core_8.0.25-1ubuntu16.04_amd64.deb
dpkg -i mysql-community-server_8.0.25-1ubuntu16.04_amd64.deb #设置密码 #选择加密方式
dpkg -i mysql-community-server_8.0.25-1ubuntu16.04_amd64.deb
service mysql restart
service mysql status
注:把以上脚本复制在text文件内,以sctipt.sh命名,然后执行即可
三、初始化Mysql配置
执行如下命令:
sudo mysql_secure_installation
终端输出如下这些命令,对照选择操作即可:
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N
#2
Please set the password for root here...
New password: 输入你需要设置的密码
Re-enter new password: 重复输入你需要设置的密码
#3
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N
#4
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network...
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N
#6
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
四、重启服务 systemctl restartmysql.service
查看服务状态 systemctl status mysql.service
mysql服务正常:
五、配置远程访问
打开终端,以root用户登录mysql,命令执行如下:
mysql -uroot -p你的数据库密码
#登录成功之后,输入
use mysql;
#新建用户
create user 'test'@'%' identified by '你的数据库密码';
#授权
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
#刷新权限
flush privileges;
#授权远程
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '你的数据库密码';
#刷新权限
flush privileges;
然后,输入exit 退出
六、修改 mysql 的配置文件
以root权限打开终端, 执行命令:
vim /etc/mysql/mysql.conf.d
查找 “bind-address = 127.0.0.1” ,注释掉
在下面一行复制上一行内容,并修改为 bind-address = 0.0.0.0
改好之后保存配置即可;
然后重启一下mysql服务
systemctl restart mysql.service
显示状态正常,就可以用数据库管理工具连接和远程操作了
常用Navicat Premium
新建连接,输入系统主机ip、端口和用户名、密码
测试连接
成功咯!
如若本文章对您有帮助,请点个小赞,创作不易,以示鼓励!