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才算卸载后安装成功。
第一套组合拳: