MySQL筆記

MySQL

注 : 有些操做和操做系統有關。mysql

CLI登陸

mysql -h localhost -u root -p

#### 示例
bovenson@MBP:~/Git/notes/MySQL$ mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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>

啓動/中止

### Mac
sudo /usr/local/mysql/support-files/mysql.server start    # 啓動
sudo /usr/local/mysql/support-files/mysql.server stop    # 中止
sudo /usr/local/mysql/support-files/mysql.server restart    # 重啓

修改密碼

SET PASSWORD

# set password for 用戶名@localhost = password('新密碼'); 
set password for root@localhost = password('123');

系統用戶

添加

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypass';
CREATE USER 'myuser'@'%' IDENTIFIED BY 'mypass';

賦予權限

GRANT ALL ON *.* TO 'myuser'@'localhost';
GRANT ALL ON *.* TO 'myuser'@'%';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

容許遠程鏈接

設置bind-address

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 修改bind-address
bind-address = 0.0.0.0

添加/修改帳戶

添加/修改帳戶容許的Host%sql

命令行遠程登陸mysql服務器

mysql -u root -p -h 10.154.0.43 -P 3306

workbench

時間設置默認當前

默認設置爲CURRENT_TIMESTAMPshell

添加管理帳戶

錯誤

Can't get hostname from your ip address

Just add below in my.ini or my.cnf.vim

[mysqld]
skip-name-resolve

CentOS安裝MySQL

# 下載 rpm 源
$ wget https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm

# 安裝 rpm 源
$ yum localinstall mysql57-community-release-el7-11.noarch.rpm

# 安裝 MySQL
$ yum install mysql-community-server

# 啓動 MySQL
$ systemctl start mysqld

# 開機啓動
$ systemctl enable mysqld
$ systemctl daemon-reload

# 獲取root密碼
$ cat /var/log/mysqld.log | grep password
... [Note] A temporary password is generated for root@localhost:  ...

# 登陸mysql
$ mysql -u root -p    
# 輸入獲取到的臨時密碼

# 重置root密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '{password}'; 

# 新建遠程登陸用戶
mysql> CREATE USER 'root'@'%' IDENTIFIED BY '{password}';

# 受權
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;

# 刷新權限
mysql> FLUSH PRIVILEGES;

# 防火牆放行3306端口
$ firewall-cmd --zone=public --add-port=3306/tcp --permanent
$ firewall-cmd --reload
相關文章
相關標籤/搜索