ubuntu環境中mysql的安裝與解除安裝

2020-08-14 17:13:01

安裝過程

sudo apt-get install mysql-server          //伺服器端
sudo apt-get install mysql-client          //用戶端
sudo apt-get install libmysqlclient-dev    //程式編譯時鏈接的庫

常用命令

service mysql start 			//啓動mysql服務
service mysql stop				//關閉mysql服務
sudo netstat -tap | grep mysql   //檢視mysql節點listen狀態
mysql -u root -p			    //進入mysql shell介面

設定root使用者密碼

xt@xt-virtual-machine:~$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = vtUHxfg5TyjolR2l
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = vtUHxfg5TyjolR2l
socket   = /var/run/mysqld/mysqld.sock

xt@xt-virtual-machine:~$ mysql -u debian-sys-maint -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31-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> use mysql;                   #連線到mysql數據庫

mysql> update mysql.user set authentication_string=password('123456') where user='root' and Host ='localhost';    #修改密碼123456是密碼

mysql> update user set  plugin="mysql_native_password";     

mysql> flush privileges;

mysql> quit;
service mysql restart

重置root密碼

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
skip-grant-tables 
service mysql restart

常用操作

show databases					//檢視所有數據庫,mysql數據庫用來新增使用者修改密碼
use mysql					    //開啓相關庫
show tables						//檢視該庫中的所有表
describe 						//檢視錶結構

解除安裝 mysql

sudo rm /var/lib/mysql/ -R 		//刪除mysql的數據檔案
sudo rm /etc/mysql/ -R 			//刪除mysql的組態檔
sudo apt-get autoremove mysql* --purge	//自動解除安裝mysql(包括server和client)
sudo apt-get remove apparmor
dpkg --list|grep mysql			//檢視MySQL的依賴項
sudo apt-get remove dbconfig-mysql
sudo apt-get remove mysql-client
sudo apt-get remove mysql-client-5.7
sudo apt-get remove mysql-client-core-5.7
sudo apt-get autoremove mysql* --purge
dpkg --list|grep mysql
sudo apt-get remove php7.0-mysql
dpkg -l|grep ^rc|awk '{print$2}'|sudo xargs dpkg -P		//清除殘留數據
dpkg --list|grep mysql							      //再次檢視MySQL的剩餘依賴項