Ubuntu18.04安装和使用Mysql数据库

本文详细介绍了在Ubuntu18.04上安装Mysql数据库的8步流程,包括如何找回root用户密码,卸载与重新安装Mysql,以及使用Python进行数据库操作,如创建数据库、表,执行增删改查操作。
摘要由CSDN通过智能技术生成

1、共8个命令即可完成安装

参考

命令1

下载安装mysql的服务端:

sudo apt-get install mysql-server
命令2

切换成root用户后执行下面的命令,否则可能权限不够,想换回普通用户只需ctrl+D

sudo su -    
命令3

下载安装mysql的客户端:

sudo apt-get install mysql-client
命令4
sudo apt-get install libmysqlclient-dev
命令5

安装netstat

sudo apt install net-tools  
命令6

用netstat查看mysql是否安装成功

sudo netstat -tap | grep mysql  

结果如下:

root@dj:~# sudo netstat -tap | grep mysql
tcp        0      0 localhost:mysql         0.0.0.0:*               LISTEN      31100/mysqld 
命令7

查看mysql工作状态,如果没有成功,输入命令:sudo systemctl start mysql尝试

systemctl status mysql.service 

结果如下:

root@dj:~# systemctl status mysql.service
● mysql.service - MySQL Community Server
   Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-05-01 15:06:16 CST; 2h 28min ago
 Main PID: 31100 (mysqld)
    Tasks: 27 (limit: 4915)
   CGroup: /system.slice/mysql.service
           └─31100 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid

5月 01 15:06:15 dj systemd[1]: Starting MySQL Community Server...
5月 01 15:06:16 dj systemd[1]: Started MySQL Community Server.
命令8
mysql -u root -p #进入数据库

其中 -u 表示选择登陆的用户名,-p 表示登陆的用户密码,输入密码后就可以登录到mysql中,第一次登录,密码为空,啥都不用输入,直接回车,就好了。

结果如下:

root@dj:~# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> 

2、找回root用户的登录密码

参考

Ubuntu在安装MySQL时会为用户创建一个debian-sys-maint用户我们可以通过这个用户来设置

2.1 查看debian-sys-maint密码
sudo cat /etc/mysql/debian.cnf

出现以下内容,注意里面包含密码:

dj@dj:~$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = WticbBDLIn7Irj3W
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = WticbBDLIn7Irj3W
socket   = /var/run/mysqld/mysqld.sock
2.2 登录debian-sys-maint用户
mysql -u debian-sys-maint -p

注意:此时密码为刚刚查到的密码

现在我们已经登录到MySQL了

2.3 设置root密码
use mysql;

update mysql.user set authentication_string=password('b307b307') where user='root' and Host ='localhost';

update user set plugin='mysql_native_password';

flush privileges;

quit;

然后重新登录:

mysql -u root -p #进入数据库

出现以下内容,表明成功登入:

dj@dj:~$ mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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> 

OK! 问题解决了

3、卸载Mysql并重新安装

三套组合拳打完Mysql才算卸载后安装成功。

第一套组合拳:

  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值