一、彻底卸载以前的 mysql
sudo apt-get autoremove --purge mysql-server
sudo apt-get remove mysql-common
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
卸完之后输入
sudo apt-get update
完成之后输入
mysql --version
如果还能显示之前安装的 mysql 的版本号,则 再来一遍上述操作,直到查不到 sql 的版本号
二、安装 mysql
参考博客地址Ubuntu19.04 安装 MySQL 8.0.16
sudo apt-get update
sudo apt-get install mysql-server
①配置初始化信息
sudo mysql_secure_installation
过程中遇到的选择
Press y|Y for Yes, any other key for No: N
选 N,不会进行密码的强校验
之后设定密码
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N
选 N,不删除匿名用户
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N
选择N,允许root远程连接
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N
选择N,不删除test数据库
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y
选择Y,修改权限立即生效
最后会出现一个 All done!的提示
②配置访问权限
sudo mysql -uroot -p
use mysql;
select User,authentication_string,Host from user;
访问权限说明:host
默认都是localhost
访问权限
第二个root
为你给新增权限用户设置的密码,%
代表所有主机,也可以是具体的ip;
grant all privileges on *.* to `root`@`%` identified by `root`
如果遇到
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘identified by ‘root’’ at line 1
说明输入的是单引号不是反引号,即按键 1 左边的那个键。
flush privileges;
③新建数据库和用户
CREATE DATABASE studentService;
GRANT ALL PRIVILEGES ON teacher.* TO studentService@% IDENTIFIED BY "admin";
④mysql 的相关命令
检查服务命令
systemctl status mysql.service
sudo service mysql status
正常状态的示例:
ysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020-06-09 11:56:03 CST; 37min ago
Main PID: 20656 (mysqld)
Status: “Server is operational”
Tasks: 39 (limit: 2310)
Memory: 317.0M
CGroup: /system.slice/mysql.service
└─20656 /usr/sbin/mysqld
Jun 09 11:56:02 ubuntu systemd[1]: Starting MySQL Community Server…
Jun 09 11:56:03 ubuntu systemd[1]: Started MySQL Community Server.
mysql 服务启动/停止
sudo service mysql stop
sudo service mysql start
终端进入 mysql
mysql -uroot -p
再输入密码即可
(-uroot
之间没有空格)
⑤使用 workbench 连接数据库