mysql的簡單操作

來源:互聯網
上載者:User

標籤:mysql

##mysql簡單操作###
#在mysql中一般用";"做結尾。
(1)yum install mariadb-server -y      ##安裝軟體
systemctl start mariadb            ##開啟服務
mysql_secure_installation          ##第一次安裝mysql,因為剛進入mysql時,是沒有密碼的,可以可以通過此條>命令對mysql進行操作,增加密碼。
mysql -u root -predhat           ##以超級使用者身份進入mysql,密碼時redhat
SHOW DATABASES;           ##顯示資料庫
SHOW TABLES;              ##顯示表
USE mysql;                ##進入mysql資料庫
SETECT * FROM mysql.user   ##列出mysql使用者中的所有資訊
DESC westos;              ##查看westos表的資料結構
FLUSH PRIVILEGES;          ##重新整理資料庫資訊
SELECT Host,User,Password FROM user; 從user表中篩選Host,User,Password資訊
(2)使用者權限設定
mysql -u root -p
Enter password:
 SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
USE mysql        
CREATE USER [email protected] identified by ‘redhat‘;      ##建立使用者redhat 密碼 redhat
GRANT INSERT on *.* to [email protected];                   ##使用者redhat在本地有插入許可權
GRANT CREATE on *.* to [email protected];                   ##使用者redhat在本地有建立許可權             
SHOW GRANTS FOR [email protected]                           ##使用者redhat在本地的所有許可權
FLUSH PRIVILEGES;                                          ## 重新整理資料庫資訊
REVOKE CREATE on *.* from [email protected];                 ##移除使用者redhat在本地的建立許可權
REVOKE INSERT on *.* from [email protected];                 ##移除使用者redhat在本地的插入許可權
DROP USER [email protected];                                 ##移初使用者redhat
(3)修改密碼(原密碼丟失)
systemctl stop mariadb.service                             ##關閉mariadb.service
mysqld_safe --skip-grant-tables &                          ##跳過grant-tables授權表  不需要認證登入本地mysql資料庫
mysql -uroot                                               ##進入資料庫時不要求輸入密碼
SELECT * FROM mysql.user;
 UPDATE mysql.user set Password=passwd(‘westos‘) WHERE User=‘root‘;  更新mysql.user 表中條件為root使用者的密碼為加密westos
ps aux | grep mysql                                       ##查看mysql進程
 killall -9 mysqld_safe                                   ##關閉進程
ps aux | grep mysql
avahi      480  0.0  0.0  30028  1476 ?        Ss   20:06   0:00 avahi-daemon: running [mysql-server.local]
mysql     2550  0.0  4.7 843940 90168 pts/0    Sl   20:54   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      2758  0.0  0.0 112640   940 pts/0    R+   21:04   0:00 grep --color=auto mysql
 kill -9 2550
 ps aux | grep mysql
avahi      480  0.0  0.0  30028  1476 ?        Ss   20:06   0:00 avahi-daemon:running [mysql-server.local]
root      2760  0.0  0.0 112640   940 pts/0    R+   21:05   0:00 grep --color=auto mysql
systemctl start mariadb                               ##開啟服務
(4)修改密碼(原密碼存在)
mysqladmin -uroot -pwestos password redhat            ##將密碼westos改為redhat

(5)資料庫的建立以及表的建立和記錄完善
CREATE DATABASE westos;                               ##建立資料庫
SHOW DATABASES;                                       ##列出資料庫
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| westos             |
+--------------------+
4 rows in set (0.00 sec)
use westos;                                        ##進入westos資料庫
Database changed
CREATE TABLE redhat (username varchar(10) not null, password varchar(20) not null, class varchar(10));    ##建立表redhat 表頭username(不可為空),password(不可為空),class
Query OK, 0 rows affected (0.02 sec)

DESC redhat        ##顯示redhat表結構
    -> ;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(10) | NO   |     | NULL    |       |
| password | varchar(20) | NO   |     | NULL    |       |
| class    | varchar(10) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
INSERT INTO redhat VALUES (‘lee‘,‘abc‘,‘01‘);     ##給表中插入記錄
INSERT INTO redhat VALUES (‘wee‘,‘abc‘,‘02‘);
(6)資料備份
mysqldump -uroot -predhat mysql > /mnt/my.sql 備份mysql庫到/mnt/my.sql
mysql -uroot -predhat westos < /mnt/my.sql 恢複/mnt/my.sql 到westos庫




mysql的簡單操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.