方法一夭咬、使用源 安裝
1、使用命令安裝mysql
#更新源
sudo apt-get update
#安裝msql-server
sudo apt-get install mysql-server
2赃额、初始化配置
安裝的是最新版本mysql8.x
安裝完成后加派,現(xiàn)在啟用 MySQL(系統(tǒng)啟動時自動啟動),啟動 MySQL跳芳,并使用以下命令驗證狀態(tài):
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld
在初始化配置(sudo mysql_secure_installation)前先設(shè)置root密碼芍锦,否則會報以下錯:
Re-enter new password:
... Failed! Error: SET PASSWORD has no significance for user 'root'@'localhost' as the authentication method used doesn't store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.
root@myubuntu:~# sudo mysql
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)
#退出
mysql> quit;
Bye
然后再初始化配置:
sudo mysql_secure_installation
1
ubuntu@i-kh37hnyf:~$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: Y(選擇N ,不會進行密碼的強校驗)
2
#已經(jīng)設(shè)置過密碼了,這步會直接略過飞盆!
Please set the password for root here...
New password: (輸入密碼)
Re-enter new password: (重復輸入)
3
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
4
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : N
... skipping.
5
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N(選擇N娄琉,不刪除匿名用戶)
... skipping.
6
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : N(選擇N,允許root遠程連接)
... skipping.
7
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N(選擇N吓歇,不刪除test數(shù)據(jù)庫)
... skipping.
8
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y(選擇Y孽水,修改權(quán)限立即生效)
Success.
All done!
3、配置root遠程登錄
3.1 修改bind-address城看,默認mysql只能本地訪問
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#找到 bind-address: 127.0.0.1 并注釋掉
#重啟mysql
sudo /etc/init.d/mysql restart
3.2 root登錄
#查詢用戶
mysql> use mysql;
mysql> select host,user,plugin from user;
#設(shè)置權(quán)限與密碼 #使用mysql_native_password修改加密規(guī)則
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
#設(shè)置root密碼永不過期
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
#允許遠程訪問
mysql> UPDATE user SET host = '%' WHERE user = 'root';
#刷新權(quán)限
mysql> flush privileges;
4女气、新增用戶
8.0之后的mysql不支持 授權(quán)的時候就進行用戶創(chuàng)建,所以創(chuàng)建 之后才能授權(quán)测柠;
#創(chuàng)建用戶
mysql> create user 'abcd'@'localhost' identified by '123456';
#給用戶賦權(quán)限
mysql> grant all privileges on *.* to 'abcd'@'localhost';
#新創(chuàng)建的用戶炼鞠,加密方式為:caching_sha2_password
#修改加密方式
mysql> alter user 'abcd'@'localhost' identified with mysql_native_password by '123456';
mysql> flush privileges;
5、root用戶登錄不了怎么辦鹃愤?
mysql有一個默認用戶:debian-sys-maint
在這個文件中:/etc/mysql/debian.cnf
sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint(這個就是默認用戶)
password = X4k1p11iFNCzUGQn (這個就是密碼)
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = X4k1p11iFNCzUGQn
socket = /var/run/mysqld/mysqld.sock
然后我們就可以用這個文件里的默認用戶登錄簇搅,并修改root密碼就可以了
mysql -udebian-sys-maint -pX4k1p11iFNCzUGQn
6、mysql常用命令
#1软吐、檢查服務(wù)狀態(tài)
systemctl status mysql.service
#或
sudo service mysql status
#2 服務(wù)啟動
sudo service mysql start
#停止
sudo service mysql stop
#3瘩将、進入mysql數(shù)據(jù)庫
mysql -u root -p
#4、退出mysql
mysql> quit;
#5、查看數(shù)據(jù)庫版本
mysql> status;
7姿现、查看字符集
mysql> show variables like 'character%';
修改數(shù)據(jù)庫默認字符集
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#修改配置
[mysqld]
character-set-server=utf8mb4
[mysql] default-character-set=utf8mb4
[client] default-character-set=utf8mb4
8肠仪、卸載mysql
1、在終端中查看MySQL的依賴項
#在終端中查看MySQL的依賴項
dpkg --list|grep mysql #卸載
sudo apt-get remove mysql-common
sudo apt-get autoremove --purge mysql-server-8.0</pre>
2备典、清理殘留數(shù)據(jù)
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
3异旧、再次查看MySQL的剩余依賴項,繼續(xù)卸載
#再次查看MySQL的剩余依賴項:
dpkg --list|grep mysql #繼續(xù)刪除剩余依賴項:
sudo apt-get autoremove --purge mysql-apt-config
4提佣、刪除原先配置文件
sudo rm -rf /etc/mysql/ /var/lib/mysql
sudo apt autoremove
sudo apt autoreclean
#如果提示指令有誤吮蛹,就把reclean改成clean