ubuntu20 apt 源里的mysql 已經(jīng)更新到 8.0, 可以直接安裝
如果沒有,參考 https://dev.mysql.com/doc/refman/8.0/en/linux-installation.html 添加. 如:
對于 ubuntu/debian
wget https://repo.mysql.com//mysql-apt-config_0.8.19-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.19-1_all.deb
對于 centos8
wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm
rpm -ivh mysql80-community-release-el8-1.noarch.rpm
安裝
sudo apt update # 更新源
sudo apt install mysql-server #安裝
centos 只安裝 mysql-client
sudo yum install mysql
mysql 服務(wù)的狀態(tài)管理
systemctl status mysql # 查看狀態(tài),裝完后默認(rèn)就啟動了,默認(rèn)開機(jī)啟動
sudo systemctl disable mysql # 關(guān)閉開機(jī)啟動
sudo systemctl disable mysql # 設(shè)置開機(jī)啟動
sudo systemctl start mysql # 啟動 mysql 服務(wù)
sudo systemctl stop mysql # 關(guān)閉 mysql 服務(wù)
默認(rèn)用戶
sudo mysql # 使用 root 用戶連入 mysql, 默認(rèn)不需要密碼
sudo cat /etc/mysql/debian.cnf # 這里提供了另一個默認(rèn)賬戶和密碼 debian-sys-maint,密碼是明文,只能在本地登錄
創(chuàng)建用戶并授權(quán),修改密碼,刪除用戶
創(chuàng)建用戶并授權(quán)(默認(rèn)使用 caching_sha2_password 加密方式,新引入的)
create user '用戶名'@'localhost' identified by '明文密碼';
grant all privileges on *.* to '用戶名'@'localhost' with grant option;
flush privileges;
修改密碼,指定使用 mysql_native_password 加密方式(mysql5.7/5.6使用的方式)
ALTER USER '用戶名'@'localhost' IDENTIFIED WITH mysql_native_password BY '明文密碼';
修改密碼,指定使用 caching_sha2_password 加密方式(mysql8使用的方式)
alter user 'root'@'localhost' identified WITH caching_sha2_password BY 'root' PASSWORD EXPIRE NEVER;
刪除用戶
DROP USER '用戶名'@'localhost';
是否用戶允許在某些ip登錄,修改 localhost
允許ip段的寫法類似 '192.168.1.%'
這里的授權(quán)語句 grant, 如果省去 @'xxx', 會報錯:
ERROR 1410 (42000): You are not allowed to create a user with GRANT
寫成帶 @ 限制ip的形式就好
允許遠(yuǎn)程連接
mysql默認(rèn)只能從本地登錄,允許從遠(yuǎn)程登錄需要修改綁定地址.
修改配置文件,綁定ip修改為 0.0.0.0
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
bind-address = 0.0.0.0
重啟mysql服務(wù)
sudo systemctl restart mysql.service
創(chuàng)建數(shù)據(jù)庫
create database dbname DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
指定這個數(shù)據(jù)庫的默認(rèn)字符編碼 utf8mb4 編碼, 和默認(rèn)排序比較方案. ci: case-insensitive, 大小寫不敏感的, cs: case-sensitive, 大小寫敏感的
卸載 mysql
sudo rm /var/lib/mysql/ -R
sudo rm /etc/mysql/ -R
sudo apt autoremove mysql* --purge