Ubuntu19.04 安装 MySQL 8.0.16
Ubuntu19.04 安装 MySQL 8.0.16
环境信息:
OS:Ubuntu19.04
OpenJDK:8
MySQL: 8.0.16
在Ubuntu中,默认情况下,只有最新版本的MySQL包含在APT软件包存储库中,要安装它,只需更新服务器上的包索引并安装默认包apt-get。
1. 添加 MySQL APT Repository
把MySQL APT repository 添加至系统的软件仓库列表里,需要下载mysql-apt-config包;
下载页面:https://dev.mysql.com/downloads/file/?id=487007
点击安装后进行2的步骤。
2.通过apt 安装MySQL
#命令1
#命令1 sudo apt-get update #命令2 sudo apt-get install mysql-server
3. 配置MySQL
3.1 配置初始化信息
sudo mysql_secure_installation
配置说明:
weison@ubuntu19-04:~$ sudo mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: N(选择N,不会进行密码的强校验) Please set the password for root here. New password: Re-enter new password: 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. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : N(选择N,不删除匿名用户) ... skipping. 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) : N(选择N,允许root远程连接) ... skipping. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N(选择N,不删除test数据库) ... skipping. 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(选择Y,修改权限立即生效) Success. All done!
3.2 配置访问权限
在Ubuntu下MySQL缺省是只允许本地访问的,使用workbench连接工具是连不上的;
如果你要其他机器也能够访问的话,需要进行配置;
3.2.1 配置远程访问
# 登陆mysql sudo mysql -uroot -p #切换数据库 use mysql; #查询用户表命令: select User,authentication_string,Host from user;
访问权限说明:host默认都是localhost访问权限
#设置权限与密码 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root'; #刷新cache中配置 flush privileges;
第二个'root'为你给新增权限用户设置的密码,%代表所有主机,也可以是具体的ip;
3.2.2 新建数据库和用户
这个之前就建好了,所以上边查询User的时候能看到:
##1 创建数据库studentService CREATE DATABASE studentService; ##2 创建用户teacher(密码admin) 并赋予其studentService数据库的远程连接权限 GRANT ALL PRIVILEGES ON teacher.* TO studentService@% IDENTIFIED BY "admin";
3.2.3 修改root账号密码:
mysql -uroot -p; use mysql; user set password=PASSWORD('新密码') where user='root'; FLUSH PRIVILEGES;
如果你的mysql是5.7版本后的
use mysql; update user set authentication_string=PASSWORD('123456') where user='root'; FLUSH PRIVILEGES;
4.mysql服务命令
4.1 检查服务状态
systemctl status mysql.service 或 sudo service mysql status
显示如下结果说明mysql服务是正常的:
weison@ubuntu19-04:~$ systemctl status mysql.service ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2019-06-29 11:10:10 CST; 15min ago Main PID: 8545 (mysqld) Tasks: 28 (limit: 4915) Memory: 173.4M CGroup: /system.slice/mysql.service └─8545 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid 6月 29 11:10:10 ubuntu19-04 systemd[1]: Starting MySQL Community Server... 6月 29 11:10:10 ubuntu19-04 systemd[1]: Started MySQL Community Server.
4.1 mysql服务启动停止
#停止 sudo service mysql stop #启动 sudo service mysql start
5.使用workbench连接数据库
用第3步新建的用户teacher登陆数据库studentService,root用户我是咋都没登陆进取。
5.1 打开workbench进行连接配置:
5.2 配置完成后,执行sql:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix