Ubuntu上安装,删除MySQL

在Linux环境上操作MySQL服务,相信对MySQL初学者来说,是一个挑战,那么本篇文章将向大家介绍如何在Ubuntu上卸载,安装MySQL服务,并介绍一些简单管理MySQL服务的操作。

卸载MySQL服务

有时候可能因为某写原因,MySQL没有安装好,导致很多人会产生卸载MySQL数据库,重新安装的想法(这种情况大家还是少遇到好),那么在Linux下如何进行卸载MySQL服务

删除MySQL数据文件

从上面MySQL的配置信息可以看到,MySQL的数据文件目录,那么在删除MySQL数据文件的时候就可以进行删除了.

1
sudo rm /var/lib/mysql/ -R

删除MySQL配置文件

1
sudo rm /etc/mysql/ -R

删除MySQL服务(包括Server和Client)

执行如下的命令

1
2
ubuntu@VM-0-8-ubuntu:/etc$ sudo apt-get autoremove mysql* --purge
ubuntu@VM-0-8-ubuntu:/etc$ sudo apt-get remove apparmor

如果出现下面的问题

1
2
3
4
5
6
7
......
Package 'mysql-testsuite-5.7' is not installed, so not removed
You might want to run 'apt-get -f install' to correct these:
The following packages have unmet dependencies:
libmysqlclient20 : Depends: mysql-common (>= 5.5) but it is not going to be installed
libmysqld-dev : Depends: libmysqlclient-dev (= 5.7.17-1ubuntu16.10) but 5.7.22-0ubuntu0.16.04.1 is to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).

说明在安装MySQL的时候,可能是安装的其他软件包不兼容导致了安装包出错时,根据提示,可以通过apt-get -f install 来进行修复sudo apt-get -f install 是修复损坏的软件包,尝试卸载出错的包,重新安装正确版本的-f是参数放在install前面跟后面是一样的效果

执行下面的命令之后,对有刚才的冲突进行解决.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
ubuntu@VM-0-8-ubuntu:/etc$ sudo apt-get --fix-broken install
Reading package lists... Done
Building dependency tree
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
libmysqld-dev
The following packages will be upgraded:
libmysqld-dev
1 upgraded, 0 newly installed, 0 to remove and 11 not upgraded.
5 not fully installed or removed.
Need to get 0 B/6,761 kB of archives.
After this operation, 122 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 105598 files and directories currently installed.)
Preparing to unpack .../libmysqld-dev_5.7.22-0ubuntu0.16.04.1_amd64.deb ...
Unpacking libmysqld-dev (5.7.22-0ubuntu0.16.04.1) over (5.7.17-1ubuntu16.10) ...
Setting up linux-base (4.5ubuntu1~16.04.1) ...
Setting up libaio-dev (0.3.110-2) ...
Setting up libnuma-dev:amd64 (2.0.11-1ubuntu1.1) ...
Setting up libmysqlclient20:amd64 (5.7.22-0ubuntu0.16.04.1) ...
Setting up libmysqlclient-dev (5.7.22-0ubuntu0.16.04.1) ...
Setting up libmysqld-dev (5.7.22-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...

冲突解决之后,再执行上面的命令就可以了.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
ubuntu@VM-0-8-ubuntu:/etc$ sudo apt-get autoremove mysql* --purge
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting 'mysqltcl' for glob 'mysql*'
Note, selecting 'mysql-mmm-agent' for glob 'mysql*'
Note, selecting 'mysql-workbench' for glob 'mysql*'
Note, selecting 'mysql-client-5.0' for glob 'mysql*'
Note, selecting 'mysql-client-5.1' for glob 'mysql*'
......
......
Package 'mysql-server-5.7' is not installed, so not removed
Package 'mysql-server-core-5.7' is not installed, so not removed
Package 'mysql-source-5.7' is not installed, so not removed
Package 'mysql-testsuite' is not installed, so not removed
Package 'mysql-testsuite-5.7' is not installed, so not removed
The following packages will be REMOVED:
libaio-dev* libmysqlclient-dev* libmysqlclient20* libmysqld-dev* libnuma-dev* mysql-client* mysql-common* mysql-community-client* mysql-community-server* mysql-community-source*
0 upgraded, 0 newly installed, 10 to remove and 10 not upgraded.
After this operation, 464 MB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 105596 files and directories currently installed.)
Removing libmysqld-dev (5.7.22-0ubuntu0.16.04.1) ...
Removing libaio-dev (0.3.110-2) ...
Removing libmysqlclient-dev (5.7.22-0ubuntu0.16.04.1) ...
Removing libmysqlclient20:amd64 (5.7.22-0ubuntu0.16.04.1) ...
Removing libnuma-dev:amd64 (2.0.11-1ubuntu1.1) ...
Removing mysql-community-server (5.7.17-1ubuntu16.10) ...
update-alternatives: warning: alternative /etc/mysql/my.cnf.fallback (part of link group my.cnf) doesn't exist; removing from list of alternatives
update-alternatives: warning: alternative /etc/mysql/mysql.cnf (part of link group my.cnf) doesn't exist; removing from list of alternatives
update-alternatives: warning: /etc/alternatives/my.cnf is dangling; it will be updated with best choice
Purging configuration files for mysql-community-server (5.7.17-1ubuntu16.10) ...
.
Removing mysql-client (5.7.17-1ubuntu16.10) ...
Removing mysql-community-client (5.7.17-1ubuntu16.10) ...
Removing mysql-common (5.7.22-0ubuntu0.16.04.1) ...
Purging configuration files for mysql-common (5.7.22-0ubuntu0.16.04.1) ...
Removing mysql-community-source (5.7.17-1ubuntu16.10) ...
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for libc-bin (2.23-0ubuntu10) ...
ubuntu@VM-0-8-ubuntu:/etc$
`

在执行该条命令之后,会弹出提示框,确认是否删除MySQL服务, 并且显示出,会删除的文件会有哪些.
进行确认之后,选择YES,那么就将MySQL进行卸载了

检查是否卸载干净

1
2
3
4
5
6
7
ubuntu@VM-0-8-ubuntu:/etc$ mysql
The program 'mysql' can be found in the following packages:
* mysql-client-core-5.7
* mariadb-client-core-10.0
Try: sudo apt install <selected package>
ubuntu@VM-0-8-ubuntu:/etc$ dpkg -l | grep mysql
ubuntu@VM-0-8-ubuntu:/etc$

可以看到,已经没有任何任何关于MySQL的东西。说明已经卸载干净了。

安装MySQL服务

卸载好了MySQL之后,如何安装呢, 下面就介绍如何Linux下快速安装MySQL.

安装server和client

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ubuntu@VM-0-8-ubuntu:/usr$ sudo apt-get install mysql-server mysql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl
libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.0-5 libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl
libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server
mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 21 newly installed, 0 to remove and 10 not upgraded.
Need to get 18.7 MB of archives.
After this operation, 162 MB of additional disk space will be used.
Do you want to continue? [Y/n]

如果发现源获取失败的,那么就先更新本地的源,执行apt-get update,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ubuntu@VM-0-8-ubuntu:/usr$ apt-get update
Reading package lists... Done
W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)
E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)
E: Unable to lock directory /var/lib/apt/lists/
W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)
W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)
ubuntu@VM-0-8-ubuntu:/usr$ sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Get:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease [107 kB]
Get:3 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease [109 kB]
Get:4 http://mirrors.tencentyun.com/ubuntu xenial-security/main Sources [134 kB]
......
Get:22 http://mirrors.tencentyun.com/ubuntu xenial-updates/main amd64 Packages [849 kB]
Get:23 http://mirrors.tencentyun.com/ubuntu xenial-updates/main i386 Packages [763 kB]
Get:24 http://mirrors.tencentyun.com/ubuntu xenial-updates/main Translation-en [347 kB]
Get:25 http://mirrors.tencentyun.com/ubuntu xenial-updates/restricted amd64 Packages [7,556 B]
Get:26 http://mirrors.tencentyun.com/ubuntu xenial-updates/restricted i386 Packages [7,524 B]
Get:27 http://mirrors.tencentyun.com/ubuntu xenial-updates/restricted Translation-en [2,272 B]
Get:28 http://mirrors.tencentyun.com/ubuntu xenial-updates/universe amd64 Packages [691 kB]
Get:29 http://mirrors.tencentyun.com/ubuntu xenial-updates/universe i386 Packages [632 kB]
Get:30 http://mirrors.tencentyun.com/ubuntu xenial-updates/universe Translation-en [279 kB]
Get:31 http://mirrors.tencentyun.com/ubuntu xenial-updates/multiverse amd64 Packages [16.4 kB]
Get:32 http://mirrors.tencentyun.com/ubuntu xenial-updates/multiverse i386 Packages [15.5 kB]
Fetched 6,736 kB in 5s (1,218 kB/s)
Reading package lists... Done

更新好了之后,就进行下载了,下载进行安装的过程会设置mysql的root密码,设置完密码,MySQL服务就安装好了.

此时查看mysql的服务,就可以看到服务已经开启了:

1
2
3
4
5
6
7
8
9
10
ubuntu@VM-0-8-ubuntu:/usr$ service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-10-02 00:11:42 CST; 4min 33s ago
Main PID: 9141 (mysqld)
CGroup: /system.slice/mysql.service
└─9141 /usr/sbin/mysqld
Oct 02 00:11:41 VM-0-8-ubuntu systemd[1]: Starting MySQL Community Server...
Oct 02 00:11:42 VM-0-8-ubuntu systemd[1]: Started MySQL Community Server.

开启MySQL日志记录

如果想要记录每次对MySQL实例的操作记录都能够记录下来,查看是否开启了日志记录可以通过如下的命令查询:

1
2
3
4
5
6
7
8
mysql> show variables like 'general%';
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | OFF |
| general_log_file | /var/lib/mysql/VM-0-8-ubuntu.log |
+------------------+----------------------------------+
2 rows in set (0.00 sec)

上面general_loggeneral_log_file就是 是否打开的开关,和日志保存的文件目录,当然可以修改mysql日志文件的记录的文件存放地址,上面的MySQL设置,默认是没有进行开启日志记录的。

可以通过对全局进行修改,如下的命令:

1
2
3
4
5
6
7
8
9
10
11
mysql> set GLOBAL general_log='ON';
Query OK, 0 rows affected (0.01 sec)
mysql> show variables like 'general%';
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | ON |
| general_log_file | /var/lib/mysql/VM-0-8-ubuntu.log |
+------------------+----------------------------------+
2 rows in set (0.00 sec)

对MySQL服务基本管理操作

开启mysql服务

1
> service mysql start

停止mysql服务

1
> service mysql stop

重启mysql服务

1
> service mysql restart

查看mysql状态

1
service mysql status

如果输出的是如下的提示,说明MySQL服务已经停止(dead), 下面中就说明了mysql的当前状态:

1
2
3
4
5
6
7
8
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Mon 2018-10-01 22:00:02 CST; 1min 40s ago
Main PID: 1268 (code=exited, status=0/SUCCESS)
Oct 01 22:00:00 VM-0-8-ubuntu systemd[1]: Stopping MySQL Community Server...
Oct 01 22:00:02 VM-0-8-ubuntu systemd[1]: Stopped MySQL Community Server.
Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.

如果输出如下的信息,说明mysql服务是正在运行着(running).

1
2
3
4
5
6
7
8
9
10
ubuntu@VM-0-8-ubuntu:/usr$ service mysql status
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-10-02 00:11:42 CST; 4min 33s ago
Main PID: 9141 (mysqld)
CGroup: /system.slice/mysql.service
└─9141 /usr/sbin/mysqld
Oct 02 00:11:41 VM-0-8-ubuntu systemd[1]: Starting MySQL Community Server...
Oct 02 00:11:42 VM-0-8-ubuntu systemd[1]: Started MySQL Community Server.

坚持原创技术分享,您的支持将鼓励我继续创作!