1. 安装mysql 8.0.22
# 安装mysql
sudo apt-get install mysql-server
sudo apt install mysql-client
# 安装sql相关库 (c语言)
sudo apt install libmysqlclient-dev
2. 第一次登陆
2.1 设置mysql
MySQL安装随附一个名为的脚本mysql_secure_installation,可让您轻松提高数据库服务器的安全性。 调用不带参数的脚本:
sudo mysql_secure_installation
remakes:
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy.....(使用密码强度校验组件) 输入: 0
New Password:(设置新密码,并重复一遍)
Remove anonymous users (删除匿名用户) n
Disallow root login remotely(拒绝远程root账号登录) n
Remove test database and access to it(移除test数据库) n
Reload privilege tables now (现在就重新载入权限表) y
2.2 root 登陆
第一次登陆root密码是空的需要设置密码。 root权限登陆:
sudo mysql -- 直接进入mysql
2.3 修改root用户密码
use mysql;
update user set authentication_string='' where user='root'; --将字段置为空
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
-- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
-- 原因:你的密码强度不够,改为如下:
alter user 'root'@'localhost' identified with mysql_native_password by 'ROOtt@_kitefano9494258..';
-- 退出mysql
quit
-- 重启mysql服务
service mysql restart
remakes: 执行下面这句
alter user 'root'@'localhost' identified with mysql_native_password by 'ROOtt@_kitefano9494258..';
如果报错的话,看root的host是不是localhost,如果不是,就把alter user 'root'@'localhost' identified...中的localhost改成对应的host,一般是改成%。
user mysql
select user, host from user;
2.4 以root身份登录
登陆
mysql -uroot -pROOtt@_kitefano9494258..
验证
show databases;
-- 如果这个出错,解决办法就是卸载重新安装。
2.5 错误处理及解决办法
如果配置错误,想还原到刚安装时的状态。解决办法:卸载重新安装。
卸载一定要完全,不然第二次安装的mysql会残留第一次安装的配置。
彻底删除MySQL
-- 查看有哪些关于mysl的服务
dpkg --list | grep mysql
-- 删除,下面个命令的中间两个是多余的,以防万一还是执行一遍吧。
sudo apt-get autoremove --purge mysql-server-5.0
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common --很重要
-- 清理残留数据???????????????
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
-- 继续删除剩余依赖项,如:
sudo apt-get autoremove --purge mysql-apt-config
-- 清理个别文件夹里的内容:
-- 1. var/lib/下mysql的内容:
sudo rm -fr /var/lib/mysql*
-- 2. etc下mysql的内容:
sudo rm -fr /etc/mysql
-- 再次查看mysl的服务,看有没有没有删除的,有的话就删掉一般会残留mysql-client-core-8.0
dpkg --list | grep mysql
-- 如果dpkg --list | grep mysql,没有删除则删除:
sudo apt-get autoremove mysql-client-core-8.0