centos 5 6 下MySQL安裝配置過程詳解

2021-06-23 10:05:04 字數 4708 閱讀 2206

安裝和配置過程如下:

centos下安裝mysql路徑

[root@sample ~]#   cd  ~        ← 切換到主目錄進行安裝

centos下安裝mysql

[root@sample ~]# yum -y install mysql-server     ← 安裝mysql

配置mysql

[root@sample ~]# vi /etc/my.cnf   ← 編輯mysql的配置檔案

old_passwords=1 ← 找到這一行,在這一行的下面新增新的規則,讓mysql的預設編碼為utf-8(假若找不到,就在本檔案到底新增)

default-character-set = utf8   ← 新增這一行

啟動mysql服務

[root@sample ~]# chkconfig mysqld on   ← 設定mysql服務隨系統啟動自啟動

[root@sample ~]# chkconfig --list mysqld  ← 確認mysql自啟動

mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off    ← 如果2--5為on的狀態就ok

[root@sample ~]# service mysqld start   ← 啟動mysql服務

mysql 初始化環境變數

4.1, 給mysql的使用者root設定密碼

mysql在剛剛被安裝的時候,它的root使用者是沒有被設定密碼的。首先來設定mysql的root密碼

[root@sample ~]# mysql -u root ← 使用者root登入mysql伺服器

mysql> select user,host,password from mysql.user;  ← 檢視使用者資訊

mysql> set password for root@localhost=password('在這裡填入root密碼'); ← 設定root密碼

mysql> set password for root@'sample.centospublic.com'=password('在這裡填入root密碼'); ← 設定root密碼

mysql> select user,host,password from mysql.user;  ← 檢視使用者資訊

mysql> exit  ← 退出mysql伺服器

4.2,刪除匿名使用者

在mysql剛剛被安裝後,存在使用者名稱、密碼為空的使用者。這使得資料庫伺服器有無需密碼被登入的可能性。為消除隱患,將匿名使用者刪除。

mysql -u root -p   ← 通過密碼用root登入

select user,host from mysql.user;  ← 檢視使用者資訊

delete from mysql.user where user=''; ← 刪除匿名使用者

select user,host from mysql.user;  ← 檢視使用者資訊

exit  ← 退出mysql伺服器

4.3, 刪除測試用資料庫  在mysql被安裝後,存在名為test的空資料庫,將它刪除。這裡要注意的是,系統預設的還有乙個名為mysql的資料庫,它用於系統管理,所以請不要刪除。

mysql -u root -p    ← 通過密碼用root登入

show databases;  ← 檢視系統已存在的資料庫

drop database test;   ← 刪除名為test的空資料庫

show databases;  ← 檢視系統已存在的資料庫

exit  ← 退出mysql伺服器

測試mysql

下面對mysql進行測試。包括建立新使用者,以及用對關係性資料庫進行資料庫操作的指令來試著建立資料庫及資料表。這裡,新建使用者以centospub為例。

mysql -u root -p   ← 通過密碼用root登入

grant all privileges on test.* to centospublic@localhost identified by '在這裡定義密碼';

← 建立對test資料庫有完全操作許可權的名為centospub的使用者

select user from mysql.user where user='centospublic';← 確認centospub使用者的存在與否

exit  ← 退出mysql伺服器

mysql -u centospublic -p  ← 用新建立的centospub使用者登入mysql伺服器

create database test; ← 建立名為test的資料庫

show databases;  ← 檢視系統已存在的資料庫

use test; ← 連線到資料庫

create table test(num int, name varchar(50)); ← 在資料庫中建立表

insert into test values(1,'hello world!');  ← 插入乙個值到表中

select * from test;  ← 檢視資料庫中的表的資訊

update test set name='hello everyone!';  ← 更新表的資訊,賦予新的值

select * from test;  ← 檢視資料庫中的表的資訊

delete from test where num=1;  ← 刪除表內的值

select * from test;  ← 確認刪除結果

drop table test;  ← 刪除表

show tables;  ← 檢視表資訊

drop database test;  ← 刪除名為test的資料庫

show databases;  ← 檢視已存在的資料庫

exit  ← 退出mysql伺服器

刪除測試用過的遺留使用者。

mysql -u root -p ← 通過密碼用root登入

revoke all privileges on *.* from centospublic@localhost;  ← 取消centospub使用者對資料庫的操作許可權

delete from mysql.user where user='centospublic' and host='localhost';  ← 刪除centospub使用者

select user from mysql.user where user='centospublic';  ← 查詢使用者centospub,確認已刪除與否

flush privileges;  ← 重新整理,使以上操作生效

exit  ← 退出mysql伺服器

centos下 遠端登入mysql配置

mysql為了安全性,在預設情況下使用者只允許在本地登入,可是在有此情況下,還是需要使用使用者進行遠端連線,因此為了使其可以遠端需要進行如下操作:

1、允許root使用者在任何地方進行遠端登入,並具有所有庫任何操作許可權,具體操作如下:

在本機先使用root使用者登入mysql:

mysql -u root -p"youpassword" 

進行授權操作:

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

過載授權表:

flush privileges;

退出mysql資料庫:

exit

2、允許root使用者在乙個特定的ip進行遠端登入,並具有所有庫任何操作許可權,具體操作如下:

在本機先使用root使用者登入mysql:

mysql -u root -p"youpassword" 

進行授權操作:

grant all privileges on *.* to root@"172.16.16.152" identified by "youpassword" with grant option;

過載授權表:flush privileges;

退出mysql資料庫:exit

3、允許root使用者在乙個特定的ip進行遠端登入,並具有所有庫特定操作許可權,具體操作如下:

在本機先使用root使用者登入mysql:。。。

進行授權操作:

grant select,insert,update,delete on *.* to root@"172.16.16.152" identified by "youpassword";

過載授權表:flush privileges;

退出mysql資料庫:exit

4、刪除使用者授權,需要使用revoke命令,具體命令格式為:

revoke privileges on 資料庫[.表名] from user-name;

具體例項,先在本機登入mysql:   mysql -u root -p"youpassword" 

進行授權操作:

grant select,insert,update,delete on test-db to test-user@"172.16.16.152" identified by "youpassword";

再進行刪除授權操作:

revoke all on test-db from test-user;

//注:該操作只是清除了使用者對於test-db的相關授權許可權,但是這個「test-user」這個使用者還是存在。

最後從使用者表內清除使用者:

delete from user where user="test-user";

過載授權表:flush privileges;

退出mysql資料庫:exit

centos5 6下mysql資料庫定時備份

單個資料庫備份 root localhost vi mysql backup.sh db user root db passwd 123456 db host 192.168.1.110 db name ecshop the directory for story your backup file....

Centos5, 6下更改系統時間和時區

檢視日期 使用 r 引數會以數字顯示時區 date 選擇時區,用當地的時區檔案覆蓋預設的 cd usr share zoneinfo asia sudo cp shanghai etc localtime 修改時間 sudo date s 20150514 18 07 00 硬體時間操作 檢視硬體時...

在CentOS 5 6上安裝Git教程

首先安裝git依賴的一些包.yum install zlib devel yum install openssl devel yum install perl yum install cpio yum install expat devel yum install gettext devel yum...