Ubuntu 下安装卸载 MySQL

Ubuntu 下如何安装、卸载 MySQL,以及添加 Python3 支持。

安装

1
2
sudo apt-get update
sudo apt-get install mysql-server

然后,启动 MySQL 服务:systemctl start mysql

进入 MySQL :mysql -uroot -p

为 MySQL 设置密码:UPDATE mysql.user SET Password = PASSWORD('password') WHERE User = 'root';

参考链接:Install MySQL Server on Ubuntu

卸载

1
2
3
4
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
sudo rm -rf /etc/mysql /var/lib/mysql
sudo apt-get autoremove
sudo apt-get autoclean

参考:Uninstall or Completely remove mysql from ubuntu 16-04 | Linux Scripts Hub

在 Python 3 中支持 MySQL

在 Python3 中通过 import MySQLdb连接 MySQL,然而安装 MySQLdb 却不是 pip install MySQLdb,正确的做法是:

1
2
3
sudo apt-get update
sudo apt-get install python3-dev libmysqlclient-dev
sudo pip3 install mysqlclient

然后,

1
2
3
4
5
6
pi@raspberrypi:~ $ python3.5
Python 3.5.2 (default, Dec 15 2017, 15:32:37)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>>

参考:pi 3 - How to connect MySQLdb in Python 3? - Raspberry Pi Stack Exchange

觉得还不错?赞助一下~
0%