溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL數(shù)據(jù)庫安裝及配置相關(guān)

發(fā)布時間:2020-06-22 10:45:08 來源:網(wǎng)絡(luò) 閱讀:735 作者:TaoismLi 欄目:MySQL數(shù)據(jù)庫

一)Centos下安裝MySQL數(shù)據(jù)庫

安裝MySql參考網(wǎng)址: https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/?


因為MySQL官網(wǎng)有創(chuàng)建yum倉庫,所以直接以yum方式安裝會非常便捷,具體步驟如下:

  1. 配置服務(wù)器的yum倉庫:將MySQL的yum倉庫添加至服務(wù)器

????????a. 到?http://dev.mysql.com/downloads/repo/yum/地址下載

????????b. 根據(jù)服務(wù)器系統(tǒng)及版本選擇相應(yīng)的RPM包

????????c. 通過以下示例命令安裝RPM包以完成服務(wù)器yum倉庫的擴(kuò)展

????????????rpm -Uvh mysql57-community-release-el6-n.noarch.rpm

?2. 選擇要安裝的MySQL版本

????如果是安裝最新的版本則不需任何設(shè)置,如果是安裝歷史版本,則通過以下命令設(shè)置:

????a. yum repolist all | grep mysql????????//查看所有可用版本

????b. yum-config-manager --disable mysql57-community????????//disable掉5.7版本

????c. yum-config-manager --enable mysql56-community ????????//enable 5.6版本

?3. 安裝MySQL

????yum install mysql-community-server????????//運(yùn)行該命令直接安裝第2步enable的版本

? 4. 啟動MySQL服務(wù)

?????service mysqld start ?????//centos 6

?? ??systemctl start mysqld.service ?????//centos 7

?? ??service mysqld status????//查看啟動情況

? 5. 登錄MySQL數(shù)據(jù)庫

? ? ?mysql -h localhost -u root -p????????//剛安裝的MySQL數(shù)據(jù)庫的root用戶無密碼,直接回車即可登錄命令行模式

特別提醒:

如果是在Centos 7及以上版本中安裝,則這里是不能成功登錄的。

需要如下一些處理步驟:

centos 7及以上版本中安裝MySQL后,root用戶每次嘗試登錄都會生產(chǎn)一個隨機(jī)密碼存在var/log/mysqld.log文件中,因此可運(yùn)行grep "password" /var/log/mysqld.log?命令獲取到該隨機(jī)密碼:

????MySQL數(shù)據(jù)庫安裝及配置相關(guān)

然后,再運(yùn)行mysql -h localhost -u root -p命令并以隨機(jī)密碼登錄命令模式,接下來再進(jìn)行第二部分的用戶及權(quán)限管理操作。

詳細(xì)信息,請參考以下網(wǎng)址:https://blog.csdn.net/z13615480737/article/details/78906598?



二)MySQL用戶及權(quán)限管理

用戶管理參考網(wǎng)址:?https://www.cnblogs.com/fslnet/p/3143344.html


1. 初次使用為root用戶設(shè)置密碼,運(yùn)行以下命令:

????mysql> set password for 'root'@'localhost' =password('123456') ;? ? ? ?//將密碼設(shè)置為 123456


2. 用戶管理

????mysql>use mysql;

????查看

????mysql> select host,user,password from?user?;

????創(chuàng)建

????mysql> create user ?zx_root ??IDENTIFIED?by 'xxxxx'; ? //identified by 會將純文本密碼加密作為散列值存儲

????修改

????mysql>rename ? user ?feng ?to ? newuser;//mysql 5之后可以使用,之前需要使用update 更新user表

????刪除

????mysql>drop?user newuser; ? //mysql5之前刪除用戶時必須先使用revoke 刪除用戶權(quán)限,然后刪除用戶,mysql5之后drop 命令可以刪除用戶的同時刪除用戶的相關(guān)權(quán)限

????更改密碼

????mysql> set password?for?zx_root =password('xxxxxx');

?????mysql> update ?mysql.user ?set ?password=password('xxxx') ?where user='otheruser';


3. 查看用戶權(quán)限

????mysql> show grants for zx_root;

????賦予權(quán)限

????mysql> grant?select/all?on dmc_db.* ?to?zx_root;

????回收權(quán)限

????mysql> revoke ?select on dmc_db.* ?from ?zx_root; ?//如果權(quán)限不存在會報錯

?

????上面的命令也可使用多個權(quán)限同時賦予和回收,權(quán)限之間使用逗號分隔

????grant select on testdb.* to common_user@’%’;

????grant insert on testdb.* to common_user@’%’;

????grant update on testdb.* to common_user@’%’;

????grant delete on testdb.* to common_user@’%’;

????或者,用一條 MySQL 命令來替代:

????grant select, insert, update, delete on testdb.* to common_user@'%';

????如果想立即看到結(jié)果使用

????flush ?privileges ;

????命令更新?

?

????設(shè)置權(quán)限時必須給出以下信息

????1,要授予的權(quán)限

????2,被授予訪問權(quán)限的數(shù)據(jù)庫或表

????3,用戶名

????grant和revoke可以在幾個層次上控制訪問權(quán)限

????1,整個服務(wù)器,使用 grant ALL ?和revoke ?ALL

????2,整個數(shù)據(jù)庫,使用on ?database.*

????3,特點表,使用on ?database.table

????4,特定的列

????5,特定的存儲過程

?

????user表中host列的值的意義

????% ? ? ? ? ? ? ?匹配所有主機(jī)

????localhost ? ?localhost不會被解析成IP地址,直接通過UNIXsocket連接

????127.0.0.1 ? ? ?會通過TCP/IP協(xié)議連接,并且只能在本機(jī)訪問;

????::1 ? ? ? ? ? ? ? ??::1就是兼容支持ipv6的,表示同ipv4的127.0.0.1

?

?4.權(quán)限授予實例

????grant 數(shù)據(jù)庫開發(fā)人員,創(chuàng)建表、索引、視圖、存儲過程、函數(shù)。。。等權(quán)限

????grant 創(chuàng)建、修改、刪除 MySQL 數(shù)據(jù)表結(jié)構(gòu)權(quán)限。

????grant create on testdb.* to developer@’192.168.0.%’;

????grant alter on testdb.* to developer@’192.168.0.%’;

????grant drop on testdb.* to developer@’192.168.0.%’;

????grant 操作 MySQL 外鍵權(quán)限

????grant references on testdb.* to developer@’192.168.0.%’;

????grant 操作 MySQL 臨時表權(quán)限

????grant create temporary tables on testdb.* to developer@’192.168.0.%’;

????grant 操作 MySQL 索引權(quán)限

????grant index on testdb.* to developer@’192.168.0.%’;

????grant 操作 MySQL 視圖、查看視圖源代碼權(quán)限

????grant create view on testdb.* to developer@’192.168.0.%’;

????grant show view on testdb.* to developer@’192.168.0.%’;

????grant 操作 MySQL 存儲過程、函數(shù)權(quán)限

????grant create routine on testdb.* to developer@’192.168.0.%’; ????????//now, can show procedure status

????grant alter routine on testdb.* to developer@’192.168.0.%’; ????????//now, you can drop a procedure

????grant execute on testdb.* to developer@’192.168.0.%’;

????

????grant 普通 DBA 管理某個 MySQL 數(shù)據(jù)庫的權(quán)限

????grant all privileges on testdb to dba@’localhost’;

????其中,關(guān)鍵字 “privileges” 可以省略。

????

????grant 高級 DBA 管理 MySQL 中所有數(shù)據(jù)庫的權(quán)限

????grant all on *.* to dba@’localhost’;

????

????MySQL grant 權(quán)限,分別可以作用在多個層次上

????1. grant 作用在整個 MySQL 服務(wù)器上:

????grant select on *.* to dba@localhost;?????????// dba 可以查詢 MySQL 中所有數(shù)據(jù)庫中的表。

????grant all on *.* to dba@localhost; ????????????// dba 可以管理 MySQL 中的所有數(shù)據(jù)庫

????2. grant 作用在單個數(shù)據(jù)庫上:

????grant select on testdb.* to dba@localhost; ????????// dba 可以查詢 testdb 中的表。

????3. grant 作用在單個數(shù)據(jù)表上:

????grant select, insert, update, delete on testdb.orders to dba@localhost;

????4. grant 作用在表中的列上:

????grant select(id, se, rank) on testdb.apache_log to dba@localhost;

????5. grant 作用在存儲過程、函數(shù)上:

????grant execute on procedure testdb.pr_add to ’dba’@’localhost’;

????grant execute on function testdb.fn_add to ’dba’@’localhost’;

注意:

????a. 修改完權(quán)限以后 一定要刷新服務(wù),或者重啟服務(wù),刷新服務(wù)用:FLUSH PRIVILEGES。

????b.?MySQL中默認(rèn)存在一個用戶名為空的賬戶,只要在本地,可以不用輸入賬號密碼即可登錄到MySQL中。而因為這個賬戶的存在,導(dǎo)致新增的用戶無法用賬號密碼登錄,只需以root用戶登陸,然后刪掉即可。

????????

????mysql?-u?root???#?以root賬戶登錄MySQL
use?mysql???#選擇mysql庫
delete?from?user?where?User='';??#刪除賬號為空的行
flush?privileges;??#刷新權(quán)限
exit??#退出mysql

????c. 運(yùn)行下面命令使root用戶可遠(yuǎn)程登錄

????mysql>?grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;

?

權(quán)限表

權(quán)限說明
all
alter
alter routine使用alter procedure 和drop procedure
create
create routine使用create ?procedure
create temporary tables使用create temporary table
create ?user
create view
delete
drop
execute使用call和存儲過程
file使用select into outfile ?和load data infile
grant option可以使用grant和revoke
index可以使用create index 和drop index
insert
lock tables鎖表
process使用show full processlist
reload? ?使用flush
replication client服務(wù)器位置訪問
replocation slave由復(fù)制從屬使用
select
show databases
show view
shutdown使用mysqladmin shutdown 來關(guān)閉mysql
super
update
usage無訪問權(quán)限


三)創(chuàng)建遠(yuǎn)程登錄

1. 首先,授權(quán)特定用戶具有遠(yuǎn)程登錄權(quán)限,具體方法,參考上述第二部分。

2. 其次,MySQL數(shù)據(jù)庫默認(rèn)使用3306端口,需要對外開放3306端口,用戶才能從遠(yuǎn)程端登錄數(shù)據(jù)庫系統(tǒng)。

a. centos 7以下執(zhí)行以下命令對外開放3306端口:

#/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

#/etc/rc.d/init.d/iptables save

#/etc/init.d/iptables status


b. centos 7及以上執(zhí)行以下命令對外開放3306端口:

#firewall-cmd --zone=public --add-port=3306/tcp --permanent
#firewall-cmd --reload?


四)數(shù)據(jù)庫備份及恢復(fù)

備份

  1. 備份數(shù)據(jù)庫命令,兩命令沒有區(qū)別

????????mysqldump -hhostname -uusername -p databasename > backupfile.sql

????????mysqldump?-hhostname -uusername -p --add-drop-table? databasename > backupfile.sql

注:這2個命令dump的數(shù)據(jù)沒有數(shù)據(jù)庫創(chuàng)建語句,因此在恢復(fù)時,如果目標(biāo)庫不存在,需先手動創(chuàng)建!

? ?2. 同時備份多個MySQL數(shù)據(jù)庫

????????mysqldump -hhostname -uusername -p --databases databasename1 databasename2 databasenameN > backupfile.sql

? ?3. 僅僅備份表結(jié)構(gòu)

????????mysqldump? -hhostname -uusername -p --no-data --databases database1 database2 databaseN > backupfile.sql

注:這2個命令dump的數(shù)據(jù)含數(shù)據(jù)庫創(chuàng)建語句!

? ?4. 只備份數(shù)據(jù)庫中某些表

????????mysqldump -hhostname -uusername -p databasename? specify_table1 specify_table2 > backupfile.sql

? ?5. 備份所有數(shù)據(jù)庫

????????mysqldump? -hhostname -uusername -p --all-databases > backupfile.sql

? ? 6. 將數(shù)據(jù)庫壓縮備份

????????mysqldump -hhostname -uusername -p databasename? | gzip > backupfile.sql.gz


恢復(fù)

? ? 1.? 恢復(fù)數(shù)據(jù)庫命令

????????mysql -hhostname -uusername -p databasename < backupfile.sql

? ? 2. 從多個數(shù)據(jù)庫備份或所有數(shù)據(jù)庫備份中恢復(fù)

????????mysql -hhostname -uusername -p --one-database databasename < backupfile.sql????????????//恢復(fù)特定的數(shù)據(jù)庫,目標(biāo)庫必須存在

????????mysql -hhostname -uusername -p?< backupfile.sql??????????????//從多個備份中一次恢復(fù)全部數(shù)據(jù)庫,目標(biāo)庫不存在可以自動創(chuàng)建

? ? 3. 恢復(fù)壓縮的MySQL數(shù)據(jù)庫

????????gunzip < backupfile.sql.gz | mysql -hhostname -uusername -p databasename

注:

???恢復(fù)的邏輯是:

????1. 在備份后新建的表將保留;

? ? 2. 刪除了備份中的表,或者修改了備份中的數(shù)據(jù)都將恢復(fù)。




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI