CentOS安裝mysql
安裝MySQL。
[root@sample ~]# yum -y install mysql-server ← 安裝MySQL
[root@sample ~]# yum -y install php-mysql ← 安裝php-mysql
配置MySQL
[root@sample ~]#gedit /etc/my.cnf ← 編輯MySQL的配置文件
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1 ← 找到這一行,在這一行的下面添加新的規(guī)則,讓MySQL的默認(rèn)編碼為UTF-8
default-character-set = utf8 ← 添加這一行
然后在配置文件的文尾填加如下語(yǔ)句:
[mysql]
default-character-set = utf8
啟動(dòng)MySQL服務(wù)
[root@sample ~]# chkconfig mysqld on ← 設(shè)置MySQL服務(wù)隨系統(tǒng)啟動(dòng)自啟動(dòng)
[root@sample ~]# chkconfig --list mysqld ← 確認(rèn)MySQL自啟動(dòng)
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off ← 如果2--5為on的狀態(tài)就OK
[root@sample ~]#/etc/rc.d/init.d/mysqld start ← 啟動(dòng)MySQL服務(wù)
Initializing MySQL database: [ OK ]
Starting MySQL: [ OK ]
MySQL初始環(huán)境設(shè)定
[1]為MySQL的root用戶設(shè)置密碼
MySQL在剛剛被安裝的時(shí)候,它的root用戶是沒(méi)有被設(shè)置密碼的。首先來(lái)設(shè)置MySQL的root密碼。
[root@sample ~]# mysql -u root ← 用root用戶登錄MySQL服務(wù)器
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select user,host,password from mysql.user; ← 查看用戶信息
+------+------------------------------+---------------+
| user | host | password |
+------+------------------------------+---------------+
| root | localhost | | ← root密碼為空
| root | sample.centospub.com | | ← root密碼為空
| | sample.centospub.com | |
| | localhost | |
|root | % |XXX |
| | | |
+------+------------------------------+---------------+
4 rows in set (0.00 sec)
mysql> set password for root@localhost=password('在這里填入root密碼'); ← 設(shè)置root密碼
Query OK, 0 rows affected (0.01 sec)
mysql> set password for root@'sample.centospub.com'=password('在這里填入root密碼'); ← 設(shè)置root密碼
Query OK, 0 rows affected (0.01 sec)
只有設(shè)置了這個(gè)才可以,才可以通過(guò)數(shù)據(jù)庫(kù)來(lái)安裝網(wǎng)址
mysql> set password for root@'xxx'=password('xxx'); ← 設(shè)置root密碼
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host,password from mysql.user; ← 查看用戶信息
+------+--------------------------------+--------------------------+
| user | host | password |
+------+--------------------------------+--------------------------+
| root | localhost | 19b68057189b027f | ← root密碼被設(shè)置
| root | sample.centospub.com | 19b68057189b027f | ← root密碼被設(shè)置
| | sample.centospub.com | |
| | localhost | |
+------+--------------------------------+--------------------------+
4 rows in set (0.01 sec)
Bye
然后,測(cè)試一下root密碼有沒(méi)有生效。
[root@sample ~]# mysql -u root ← 通過(guò)空密碼用root登錄
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ← 出現(xiàn)此錯(cuò)誤信息說(shuō)明密碼設(shè)置成功
[root@localhost ~]# mysql -u root -h sample.centospub.com ← 通過(guò)空密碼用root登錄
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) ← 出現(xiàn)此錯(cuò)誤信息說(shuō)明密碼設(shè)置成功
[root@sample ~]#mysql -u root -p ← 通過(guò)密碼用root登錄
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g. ← 確認(rèn)用密碼能夠成功登錄
Your MySQL connection id is 5 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> exit
Bye
[root@sample ~]# mysql -u root -h sample.centospub.com -p ← 通過(guò)密碼用root登錄
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g. ← 確認(rèn)用密碼能夠成功登錄
Your MySQL connection id is 6 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> exit ← 退出MySQL服務(wù)器Bye
[2] 刪除匿名用戶
在MySQL剛剛被安裝后,存在用戶名、密碼為空的用戶。這使得數(shù)據(jù)庫(kù)服務(wù)器有無(wú)需密碼被登錄的可能性。為消除隱患,將匿名用戶刪除。
[root@sample ~]# mysql -u root -p ← 通過(guò)密碼用root登錄
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> select user,host from mysql.user; ← 查看用戶信息
+------+----------------------------+
| user | host |
+------+----------------------------+
| | localhost |
| root | localhost |
| | sample.centospub.com |
| root | sample.centospub.com |
+------+----------------------------+
4 rows in set (0.02 sec)
mysql> delete from mysql.user where user=''; ← 刪除匿名用戶
Query OK, 2 rows affected (0.17 sec)
mysql> select user,host from mysql.user; ← 查看用戶信息
+------+----------------------------+
| user | host |
+------+----------------------------+
| root | localhost |
| root | sample.centospub.com |
+------+----------------------------+
2 rows in set (0.00 sec)
Bye
好了,下面都不是必須的了!
測(cè)試MySQL
[root@sample ~]# mysql -u root -p ← 通過(guò)密碼用root登錄
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> grant all privileges on test.* to centospub@localhost identified by '在這里定義密碼'; ← 建立對(duì)test數(shù)據(jù)庫(kù)有完全操作權(quán)限的名為centospub的用戶
Query OK, 0 rows affected (0.03 sec)
mysql> select user from mysql.user where user='centospub'; ← 確認(rèn)centospub用戶的存在與否
+---------+
| user |
+---------+
| centospub | ← 確認(rèn)centospub已經(jīng)被建立
+---------+
1 row in set (0.01 sec)
mysql> exit ← 退出MySQL服務(wù)器
Bye
[root@sample ~]# mysql -u centospub -p ← 用新建立的centospub用戶登錄MySQL服務(wù)器
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database test; ← 建立名為test的數(shù)據(jù)庫(kù)
Query OK, 1 row affected (0.00 sec)
mysql> show databases; ← 查看系統(tǒng)已存在的數(shù)據(jù)庫(kù)
+-------------+
| Database |
+-------------+
| test |
+-------------+
1 row in set (0.00 sec)
mysql> use test ← 連接到數(shù)據(jù)庫(kù)
Database changed
mysql> create table test(num int, name varchar(50)); ← 在數(shù)據(jù)庫(kù)中建立表
Query OK, 0 rows affected (0.03 sec)
mysql> show tables; ← 查看數(shù)據(jù)庫(kù)中已存在的表
+-------------------+
| Tables_in_test |
+-------------------+
| test |
+-------------------+
1 row in set (0.01 sec)
mysql> insert into test values(1,'Hello World!'); ← 插入一個(gè)值到表中
Query OK, 1 row affected (0.02 sec)
mysql> select * from test; ← 查看數(shù)據(jù)庫(kù)中的表的信息
+------+-------------------+
| num | name |
+------+-------------------+
| 1 | Hello World! |
+------+-------------------+
1 row in set (0.00 sec)
mysql> update test set name='Hello Everyone!'; ← 更新表的信息,賦予新的值
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from test; ← 查看數(shù)據(jù)庫(kù)中的表的信息
+------+----------------------+
| num | name |
+------+----------------------+
| 1 | Hello Everyone! | ← 確認(rèn)被更新到新的值
+------+----------------------+
1 row in set (0.01 sec)
mysql> delete from test where num=1; ← 刪除表內(nèi)的值
Query OK, 1 row affected (0.00 sec)
mysql> select * from test; ← 確認(rèn)刪除結(jié)果
Empty set (0.01 sec)
mysql> drop table test; ← 刪除表
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; ← 查看表信息
Empty set (0.00 sec) ← 確認(rèn)表已被刪除
mysql> drop database test; ← 刪除名為test的數(shù)據(jù)庫(kù)
Query OK, 0 rows affected (0.01 sec)
mysql> show databases; ← 查看已存在的數(shù)據(jù)庫(kù)
Empty set (0.01 sec) ← 確認(rèn)test數(shù)據(jù)庫(kù)已被刪除(這里非root用戶的關(guān)系,看不到名為mysql的數(shù)據(jù)庫(kù))
Bye
然后,刪除測(cè)試用過(guò)的遺留用戶。
[root@sample ~]# mysql -u root -p ← 通過(guò)密碼用root登錄
Enter password: ← 在這里輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12 to server version: 4.1.20
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> revoke all privileges on *.* from centospub@localhost; ← 取消centospub用戶對(duì)數(shù)據(jù)庫(kù)的操作權(quán)限
Query OK, 0 rows affected (0.00 sec)
mysql> delete from mysql.user where user='centospub' and host='localhost'; ← 刪除centospub用戶
Query OK, 1 row affected (0.01 sec)
mysql> select user from mysql.user where user='centospub'; ← 查找用戶centospub,確認(rèn)已刪除與否
Empty set (0.01 sec) ← 確認(rèn)centospub用戶已不存在
mysql> flush privileges; ← 刷新,使以上操作生效
Query OK, 0 rows affected (0.01 sec)
Bye
[root@sample ~]# /etc/rc.d/init.d/httpd restart ← 重新啟動(dòng)HTTP服務(wù)
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
xxx is not in the sudoers file解決方法
用sudo時(shí)提示"xxx is not in the sudoers file. This incident will be reported.其中XXX是你的用戶名,也就是你的用戶名沒(méi)有權(quán)限使用sudo,我們只要修改一下/etc/sudoers文件就行了。下面是修改方法:
1)進(jìn)入超級(jí)用戶模式。也就是輸入"su -",系統(tǒng)會(huì)讓你輸入超級(jí)用戶密碼,輸入密碼后就進(jìn)入了超級(jí)用戶模式。(當(dāng)然,你也可以直接用root用)
2)添加文件的寫(xiě)權(quán)限。也就是輸入命令"chmod u+w /etc/sudoers"。
3)編輯/etc/sudoers文件。也就是輸入命令"vim /etc/sudoers",輸入"i"進(jìn)入編輯模式,找到這一 行:"root ALL=(ALL) ALL"在起下面添加"xxx ALL=(ALL) ALL"(這里的xxx是你的用戶名),然后保存(就是先按一 下Esc鍵,然后輸入":wq")退出。
4)撤銷(xiāo)文件的寫(xiě)權(quán)限。也就是輸入命令"chmod u-w /etc/sudoers"。
1、添加用戶,首先用adduser命令添加一個(gè)普通用戶,命令如下:
#adduser junguoguo//添加一個(gè)名為junguoguo的用戶
#passwd junguoguo //修改密碼
Changing password for user junguoguo.
New UNIX password: //在這里輸入新密碼
Retype new UNIX password: //再次輸入新密碼
passwd: all authentication tokens updated successfully.
2、賦予root權(quán)限
方法一: 修改 /etc/sudoers 文件,找到下面一行,把前面的注釋?zhuān)?)去掉
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
然后修改用戶,使其屬于root組(wheel),命令如下:
#usermod -g root junguoguo
修改完畢,現(xiàn)在可以用junguoguo帳號(hào)登錄,然后用命令 su – ,即可獲得root權(quán)限進(jìn)行操作。
方法二: 修改 /etc/sudoers 文件,找到下面一行,在root下面添加一行,如下所示:
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
junguoguo ALL=(ALL) ALL
修改完畢,現(xiàn)在可以用junguoguo帳號(hào)登錄,然后用命令 su – ,即可獲得root權(quán)限進(jìn)行操作。
方法三: 修改 /etc/passwd 文件,找到如下行,把用戶ID修改為 0 ,如下所示:
junguoguo:x:500:500:junguoguo:/home/junguoguo:/bin/bash
修改后如下
junguoguo:x:0:500:junguoguo:/home/junguoguo:/bin/bash
保存,用junguoguo賬戶登錄后,直接獲取的就是root帳號(hào)的權(quán)限。
posted on 2012-05-24 13:29 順其自然EVO 閱讀(1960) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): linux