MySQL

mycli – MySQL 語法小幫手

MyCLI is a command line interface for MySQL, MariaDB, and Percona with auto-completion and syntax highlighting.

意思就是用 MyCLI 可以有自動完成跟 syntax highlighting,真的方便又好閱讀!

直接來看看官方網站提供的範例:

安裝方式:

Ubuntu

sudo apt install mycli

Python Package:

pip3 install mycli

使用方式

先搭配先前的文章 – 使用 mysql_config_editor 避免密碼外漏,然後就可以用 config 檔來執行 MyCLI

mycli – login-path=phpmyadmin

使用 mysql_config_editor 避免密碼外漏

以往在備份 MySQL 的時候,都會把密碼寫在 script 裡頭,基本上就不安全。從某天開始 Bash shell scipt 就會跳出警告 Warning: Using a password on the command line interface can be insecure,雖然還是能夠執行備份,但是就覺得很毛,後來乾脆一次用 mysql_config_editor 修掉手邊所有的 backup script。

建立 mysql_config_editor 的 login-path

mysql_config_editor set – login-path=phpmyadmin – host=localhost – user=phpmyadmin – password

或是 root

mysql_config_editor set – login-path=root – host=localhost – user=root – password

密碼要自行輸入唷。

顯示已建立的 login-path

root@guestOS:~# mysql_config_editor print – all
[phpmyadmin]
user = phpmyadmin
password = *****
host = localhost

可以看到密碼被 ***** 了。

不用密碼使用 mysql

mysql – login-path=phpmyadmin

mysql_config_editor 相關使用方式

列出所有 login-path

mysql_config_editor print – all

清空 or 重設所有 login-path

mysql_config_editor reset

移除單一 login-path

mysql_config_editor remove – login-path=phpmyadmin

PHP Dependency of phpMyadmin & WordPress

前置作業 – Nginx & PHP & MySQL

關於 MySQL 8 的安裝請參考這篇 – Ubuntu 安裝 MySQL 8.0 (mysql-community-server)

nginx 跟 php 是用 Ondřej Surý 打包好的。

sudo add-apt-repository -n ppa:ondrej/php && sudo add-apt-repository -y ppa:ondrej/nginx-mainline

MySQL 的部份看要使用 MySQLMariaDB 或是 PERCONA 都可以。

PHP7.4

sudo apt install dbconfig-common dbconfig-mysql javascript-common libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libcurl4 libflac8 libjs-cropper libjs-jquery libjs-sphinxdoc libjs-underscore liblua5.2-0 libnghttp2-14 libpcre2-8-0 libphp-phpmailer libpsl5 librtmp1 libsodium23 libzip4 php php-bz2 php-common php-curl php-gd php-getid3 php-mbstring php-mysql php-pear php-phpseclib php-tcpdf php-xml php-zip php7.4 php7.4-bz2 php7.4-cli php7.4-common php7.4-curl php7.4-fpm php7.4-gd php7.4-fpm php7.4-json php7.4-mbstring php7.4-mysql php7.4-opcache php7.4-readline php7.4-xml php7.4-zip publicsuffix vorbis-tools

PHP8.0(把上面 7.4 都換成 8.0,沒有的直接去掉 7.4)

sudo apt install php-getid3 php8.0 php8.0-bz2 php8.0-cli php8.0-common php8.0-curl php8.0-fpm php8.0-gd php8.0-fpm php8.0-mbstring php8.0-mysql php8.0-opcache php8.0-readline php8.0-xml php8.0-zip

phpMyAdmin 前製作業

sudo mysql_secure_installation

跑完之後,設定 phpmyadmin 帳號權限

sudo mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'phpmyadmin'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;

設定 nginx

安裝 WordPress

Ubuntu 安裝 MySQL 8.0 (mysql-community-server)

有鑑於 MySQL 8 的效能跟各方面的精進,還有安全上的保護,慢慢將手上的機器都換成 MySQL 8。

首先到 MySQL Community Downloads 挑選 MySQL APT Repository,然後下載 mysql-apt-config_0.8.16-1_all.deb,接下來安裝:

wget https://repo.mysql.com//mysql-apt-config_0.8.16-1_all.deb
dpkg -i mysql-apt-config_0.8.16-1_all.deb

然後更新 apt repository list

sudo apt update

安裝 mysql-community-client mysql-community-server

sudo apt install mysql-community-client mysql-community-server

察看安裝的版本

ubuntu@guestOS:~$ dpkg -l | grep mysql-community
ii  mysql-community-client           8.0.23-1ubuntu20.04                                                amd64        MySQL Client
ii  mysql-community-client-core      8.0.23-1ubuntu20.04                                                amd64        MySQL Client Core Binaries
ii  mysql-community-client-plugins   8.0.23-1ubuntu20.04                                                amd64        MySQL Client plugin
ii  mysql-community-server           8.0.23-1ubuntu20.04                                                amd64        MySQL Server
ii  mysql-community-server-core      8.0.23-1ubuntu20.04                                                amd64        MySQL Server Core Binaires

接下來編輯 /etc/mysqlmysql.conf.d/mysqld.cnf

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

於 [mysqld] 之下,新增下列參數,不知道什麼原因,安裝好之後,只會預設 listen 在 IPv6

bind-address    = 127.0.0.1

重新啟動 MySQL

sudo systemctl restart mysql.service

接下來開始 MySQL 的設定。

sudo mysql_secure_installation

MySQL 安裝後必要步驟

https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-on-ubuntu-18-04

sudo mysql_secure_installation

SELECT user,authentication_string,plugin,host FROM mysql.user;
ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘$ComplatedPasswords’;
FLUSH PRIVILEGES;

CREATE USER ‘sammy’@’localhost’ IDENTIFIED BY ‘password’;
GRANT ALL PRIVILEGES ON *.* TO ‘sammy’@’localhost’ WITH GRANT OPTION;

SHOW VARIABLES LIKE ‘validate_password%’;
SET GLOBAL validate_password_policy=LOW;

Or we can set it in my.cnf file

[mysqld]
validate_password_policy=LOW

my.cnf

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘$ROOTPASSWORD$’;
FLUSH PRIVILEGES;
SELECT user,authentication_string,plugin,host FROM mysql.user;

SHOW VARIABLES LIKE ‘validate_password%’;

回到頂端