当前位置: 首页 > 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.jsqmd.com/news/995984/

相关文章:

  • 2026年护理专业公办大专怎么选?河南三所实力院校深度解析(附真实案例) - 优质品牌商家
  • 别再手动算植被覆盖度了!用GEE+Sentinel-2数据,5分钟搞定FVC制图(附完整代码)
  • 《老板说电费又涨了,于是我们做了一套智慧能源管理平台》
  • 第3章:从设计到演化,欢迎来到agent时代
  • 绵阳本地AI搜索优化公司行业常见服务内容与基础运营执行标准
  • 别再傻傻分不清!EPLAN里这17种‘点’到底怎么用?手把手教你从‘中断点’到‘布线点’
  • C盘满了怎么清理才安全?按顺序清空间不踩坑
  • 优先经验回放(PER)真的那么神吗?在CartPole和Atari游戏中的实战效果与调参避坑指南
  • YOLOv8保姆级调优指南:从CSPDarknet53到PANet,手把手教你提升目标检测精度
  • 一文读懂:将问题转化为欧拉路径
  • Java毕设选题推荐:基于协同过滤SpringBoot的音乐推荐系统 【附源码、mysql、文档、调试+代码讲解+全bao等】
  • 5G NR PUSCH时域资源实战:从DCI调度到Configured Grant,手把手教你读懂配置表
  • Cortex-M3/M4开发避坑指南:如何配置SCB->SHCSR使能BusFault、MemFault和UsageFault
  • 2026年当下青阳九华山家常菜馆酒楼推荐与避坑指南 - 品牌鉴赏官2026
  • 量子Walsh-Hadamard变换在信号频带检测中的应用
  • 人工智能导论——从迷宫到现实:搜索算法的核心思想与应用演进
  • 从‘并联支路’到单个元件:Simulink电力系统模块库(Specialized Power Systems)的元件使用心法
  • 3步构建企业级数据可视化大屏的完整解决方案
  • 别再死记1/jωC了!从电容充电放电的动画,带你直观理解容抗公式的物理意义
  • 硬件工程师避坑指南:芯片选型时,I/O Pad和封装参数你真的看对了吗?
  • 从数据手册到实际电路:手把手教你解读运放Vos和Ios参数,并完成精准测量与补偿
  • 解决 Alpine Linux 虚拟机从 VirtualBox 迁移到 VMware 的内核崩溃问题
  • Pentaho Kettle 11.x 架构深度解析:高性能ETL引擎的并发处理与内存优化策略
  • 5G-A+边缘计算:低延迟应用爆发的真正推手
  • 从收音机到手机:聊聊BJT这个‘老古董’是怎么在模拟电路里扛起放大重任的
  • 2026年炉渣钢渣行业深度分析:专业厂家如何选?上阳建材、天娇包装、木林森等企业实力对比 - 优质品牌商家
  • 鸿蒙导航意图 的 Flutter 侧封装思路
  • 2026重庆家装设计力榜单:十大优质设计装修公司评测与消费参考 - 互联网科技品牌测评
  • Java 创建对象有几种方式
  • 光刻、蚀刻、离子注入… 芯片厂里这些‘黑话’到底在干嘛?5分钟带你搞懂