当前位置: 首页 > news >正文

Ubuntu快速安装MySQL全攻略

这里介绍ubuntu发行版本上使用apt命令安装的步骤。不同发行版本安装方法有所不同。

一、安装mysq

安装前首先切换到管理员身份:

sudo su

然后再执行命令安装:

apt install mysql-server

stu@stu-virtual-machine:~$ sudo su [sudo] stu 的密码: root@stu-virtual-machine:/home/stu# apt install mysql-server

二、初始化配置

初始化配置使用命令:

mysql_secure_installation

1.设置数据库不进行密码强校验

root@stu-virtual-machine:/home/stu# mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT 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 component? Press y|Y for Yes, any other key for No: N (输入N,不进行密码的强校验)

2.设置root管理员密码

注意是数据库的管理员的。密码不回显,根据提示输入两遍

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. 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 ... skipping.

4.设置是否允许root用户远程登录

这里设置允许

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 ... skipping.

5.是否删除'test'

这里选择不删除

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 ... skipping.

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 Success. All done! root@stu-virtual-machine:/home/stu#

三、数据库服务启动停止

1.检查服务器状态

命令为:

service mysql status

或者

systemctl status mysql.service

root@stu-virtual-machine:/home/stu# service mysql status ● mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-06-21 13:36:08 CST; 24min ago Main PID: 4672 (mysqld) Status: "Server is operational" Tasks: 39 (limit: 4617) Memory: 335.5M CGroup: /system.slice/mysql.service └─4672 /usr/sbin/mysqld 6月 21 13:36:07 stu-virtual-machine systemd[1]: Starting MySQL Community Server... 6月 21 13:36:08 stu-virtual-machine systemd[1]: Started MySQL Community Server. root@stu-virtual-machine:/home/stu#

2.重启|停止|启动,数据库的命令

如果不是管理员需要在命令前面加上==sudo==

重启:/etc/init.d/mysqlrestart
停止:/etc/init.d/mysqlstop
启动:/etc/init.d/mysqlstart

或者执行如下命令

root@stu-virtual-machine:/home/stu# service mysql restart
root@stu-virtual-machine:/home/stu# service mysql stop
root@stu-virtual-machine:/home/stu# service mysql start

3.连接数据库命令

mysql -uroot -p

root@stu-virtual-machine:/home/stu# mysql -uroot -p Enter password: (此处输入设置的密码,如果没有密码直接回车) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu) Copyright (c) 2000, 2021, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> quit (退出数据库客户端的命令)

四、配置文件位置

配置文件在如下位置,需要修改可以用vi打开更改

root@stu-virtual-machine:/home/stu# vi /etc/mysql/mysql.conf.d/mysqld.cnf
例如,需远程登录,则需要将bind-address改为0.0.0.0,如下
1 # Instead of skip-networking the default is now to listen only on 2 # localhost which is more compatible and is not less secure. 3 bind-address = 127.0.0.1 (改为0.0.0.0)

五、安装c/c++开发库

安装开发c/c++的库,命令:

apt install libmysqlclient-dev

stu@stu-virtual-machine:~$ sudo su [sudo] stu 的密码: root@stu-virtual-machine:/home/stu# apt install libmysqlclient-dev
测试c语言连接数据库,这里连接'test',没有自己创建,注意‘111111’是数据库密码,需要输入自己的。
#include <stdio.h> #include <mysql/mysql.h> int main() { MYSQL connect;//mysql连接对象 mysql_init(&connect); //连接到mysql if(mysql_real_connect(&connect,"localhost","root","111111","test",0,NULL,0)) { printf("连接mysql成功\n"); } else { printf("err:%s\n",mysql_error(&connect)); printf("连接mysql失败\n"); } //关闭mysql连接 mysql_close(&connect); return 0; }

编译时,需要指定-l指定mysqlclient这个库:

gcc-omysqltest mysqltest.c-lmysqlclient

stu@stu-virtual-machine:~/lg$ gcc -o mysqltest mysqltest.c -lmysqlclient stu@stu-virtual-machine:~/lg$

运行程序时要注意,有些情况使系统用管理员身份运行可以成功:

root@stu-virtual-machine:/home/stu/lg# ./mysqltest 连接mysql成功 root@stu-virtual-machine:/home/stu/lg#
但普通用户可能出现失败,此时需要更改mysql数据库管理员密码,并设置好加密方式。要注意一个是 linux系统管理员root,还有一个是Mysql数据库的管理员,名字也是root。不要混淆。
stu@stu-virtual-machine:~/lg$ ./mysqltest err:Access denied for user 'root'@'localhost' 连接mysql失败 stu@stu-virtual-machine:~/lg$

六、用户管理与授权

1.查看用户信息

mysql> select user,host,plugin from mysql.user;

2.创建用户示例

mysql> create user'stu'@'localhost'identified by'Iabc_123456';

3.创建用户指定加密方式示例

mysql> create user'stu1'@'localhost'identified WITH mysql_native_password by
'Iabc_123456';

4.更新用户密码

注意密码强度大小写数字

ALTER user'root'@'localhost'IDENTIFIED WITH mysql_native_password BY
'Iabc_123456';

5.授权用户对那些数据库的那些表可以进行操作示例

指定user_name用户可以从任意地点登录访问所有数据库的所有表

GRANT ALL PRIVILEGES ON *.* TO'user_name'@'%'identified by'密码'
GRANT ALL ON database_name.table_name TO'user_name'@'localhost'

6.删除用户

drop user'name'@'localhost';
http://www.rkmt.cn/news/1507753.html

相关文章:

  • 《老板说电费又涨了,于是我们做了一套智慧能源管理平台》
  • 别小看这颗并联的小电容:前馈电容如何让你的模块电源‘快准稳’?
  • 2026年护理专业公办大专怎么选?河南三所实力院校深度解析(附真实案例) - 优质品牌商家
  • 给网卡刷个‘灵魂’:手把手带你读懂PCIe设备的Expansion ROM(以Intel 82599为例)
  • 绵阳本地AI搜索优化公司行业常见服务内容与基础运营执行标准
  • 别再傻傻分不清!EPLAN里这17种‘点’到底怎么用?手把手教你从‘中断点’到‘布线点’
  • 优先经验回放(PER)真的那么神吗?在CartPole和Atari游戏中的实战效果与调参避坑指南
  • Pentaho Kettle 11.x 架构深度解析:高性能ETL引擎的并发处理与内存优化策略
  • 鸿蒙导航意图 的 Flutter 侧封装思路
  • 进阶RAG实战:RAG吃透80%基础场景,Graph RAG攻克20%复杂业务瓶颈
  • RIGOL示波器DS6104背后接口实测:触发信号延迟40ns?输出阻抗到底是多少?
  • 光刻、蚀刻、离子注入… 芯片厂里这些‘黑话’到底在干嘛?5分钟带你搞懂
  • 字节AI布局深潜:从豆包到Trae,重构开发者生态
  • 手把手教你用PHY6222芯片的simpleBLEPeripheral例程,从广播数据到属性表一次搞懂
  • 深入浅出:图解5G NR PUSCH的Repetition Type A/B与TBoMS,到底该怎么选?
  • 告别NeRF的‘慢动作’:Instant-NGP的多分辨率哈希编码如何实现秒级训练?
  • 2026年南充广告公司口碑深度分析:谁在坚守诚信与品质? - 优质品牌商家
  • Java毕设选题推荐:基于SpringCloud的美食分享交流平台内容发布、互动交流、搜索推荐等功能【附源码、mysql、文档、调试+代码讲解+全bao等】
  • EEGNet vs. EEGNex:一次失败的注意力机制尝试与四个成功的架构改进
  • 信息孤岛困局与认知协作革命:开源 RAG 框架 FastGPT 如何重塑企业知识工程
  • 别再只改颜色了!ECharts Tooltip 高级自定义指南:从悬浮样式到动态内容生成
  • 企业团体体检攻略:HR必知的6个关键决策点
  • 常用插件引进unity方法,亲测好用
  • 高通平台UEFI开发避坑:ABL与XBL中控制GPIO的正确姿势(以关机充电为例)
  • Linux 组管理命令工具链
  • 2026年沾益区驾校学车报名条件全解析:如何选择靠谱驾校? - 品牌鉴赏官2026
  • 无人机、手机定位都离不开它:一文讲透GDOP如何影响你的位置精度
  • 111111111111111111111111111测试
  • 踩坑亏了700元!使用Codex AI编程的9条实战铁律
  • GraphRAG 技术选型:小白工程师必看,你的数据是否适合用它?(含收藏)