centOS Linux下用yum 安裝mysql

來源:互聯網
上載者:User
文章目錄
  •                                       第一篇:安裝和配置MySQL

注意:centOS Linux系統下用yum 安裝mysql,必須在網路環境下進行安裝;關於Linux系統下如何串連網路,前一篇文章有介紹,請參考!

                                      第一篇:安裝和配置MySQL

  第一步:安裝MySQL

[root@192 local]# yum -y install mysql-server ← 安裝MySQL

[root@192 local]# yum -y install php-mysql ← 安裝php-mysql

 

  第二步:配置MySQL

[root@192 local] #vim /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 ← 找到這一行,在這一行的下面添加新的規則,讓MySQL的預設編碼為UTF-8
default-character-set = utf8 ← 添加這一行

然後在設定檔的文尾填加如下語句:

[mysql]
default-character-set = utf8

 

       第二篇:啟動MySQL和初始環境設定

 

  第一步:啟動MySQL服務

[root@192 local]#chkconfig mysqld on ← 設定MySQL服務隨系統啟動自啟動

[root@192 local]#chkconfig --list mysqld ← 確認MySQL自啟動
mysqld          0:關閉  1:關閉  2:啟用  3:啟用  4:啟用  5:啟用  6:關閉

                                              ← 如果2--5為啟用(或on)的狀態就OK

[root@192 local]#/etc/rc.d/init.d/mysqld start ← 啟動MySQL服務

初始化 MySQL 資料庫: Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h 192.168.0.1 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [確定]
啟動 mysqld:                                              [確定]

 

  第二步:MySQL初始環境設定

  [1]  為MySQL的root使用者佈建密碼

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

[root@192 local]#mysql -u root  ← 在沒設定密碼之時,用root使用者登入MySQL伺服器
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.95 Source distribution

Copyright (c) 2000, 2011, 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> select user,host,password from mysql.user; ← 查看使用者資訊
mysql> select user,host,password from mysql.user;
+------+-------------+----------+
| user | host        | password |
+------+-------------+----------+
| root | localhost   |          |
| root | 192.168.0.1 |          |
| root | 127.0.0.1   |          |
|      | localhost   |          |
|      | 192.168.0.1 |          |
+------+-------------+----------+
5 rows in set (0.03 sec)

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

譬如,在我的系統中,我是如下設定:

mysql> set password for root@localhost=password ('wangyun');

Query OK, 0 rows affected (0.01 sec)

mysql> set password for root@192.168.0.1=password ('wangyun');
Query OK, 0 rows affected (0.01 sec)

mysql> set password for root@127.0.0.1=password ('wangyun');
Query OK, 0 rows affected (0.01 sec)

 

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

+------+-------------+------------------+
| user | host        | password         |
+------+-------------+------------------+
| root | localhost   | 5f2dfe4b07af795b |
| root | 192.168.0.1 | 5f2dfe4b07af795b |
| root | 127.0.0.1   | 5f2dfe4b07af795b |
|      | localhost   |                  |
|      | 192.168.0.1 |                  |
+------+-------------+------------------+
5 rows in set (0.01 sec)

mysql> exit ← 退出MySQL伺服器
Bye

 

 [2]測試設定的root密碼是否生效

 

[root@192 local]# mysql -u root      ← 通過空密碼用root登入
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
                                     ← 出現此錯誤資訊說明密碼設定成功

[root@192 local]# mysql -u root -p   ← 通過密碼用root登入
Enter password: ← 在這裡輸入密碼

Welcome to the MySQL monitor.  Commands end with ; or \g.  ← 確認用密碼能夠成功登入
Your MySQL connection id is 5
Server version: 5.0.95 Source distribution

...  ...

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

[root@192 local]# mysql -u root -h 127.0.0.1 -p ← 通過密碼用root登入
Enter password:   ← 在這裡輸入密碼
Welcome to the MySQL monitor.  Commands end with ; or \g.← 確認用密碼能夠成功登入
Your MySQL connection id is 13
Server version: 5.0.95 Source distribution

......

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit ← 退出MySQL伺服器
Bye

 

 [3] 刪除匿名使用者

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

 

[root@192 local]# mysql -u root -p  ← 通過密碼用root登入
Enter password:    ← 在這裡輸入密碼

 

mysql> select user,host from mysql.user;
+------+-------------+
| user | host        |
+------+-------------+
| root | 127.0.0.1   |
|      | 192.168.0.1 |
| root | 192.168.0.1 |
|      | localhost   |
| root | localhost   |
+------+-------------+
5 rows in set (0.03 sec)

 

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

Query OK, 2 rows affected (0.04 sec)

 

mysql> select user,host from mysql.user;  ← 查看使用者資訊

+------+-------------+
| user | host        |
+------+-------------+
| root | 127.0.0.1   |
| root | 192.168.0.1 |
| root | localhost   |
+------+-------------+
3 rows in set (0.00 sec)

 

mysql> exit;   ←退出MySQL伺服器
Bye

 

第三篇:測試

經過第一篇和第二篇這二部分的設定,MySQL就可以正常試用。

好了,第三篇的內容都不是必須的了!
我們來測試MySQL,前學習或複習常用的SQL語句。

[root@192 local]#mysql -u centospub-p;  ← 通過密碼用root登入
Enter password: ← 在這裡輸入密碼

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

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

譬如,在我的系統中,設定如下:

mysql> grant all privileges ontest.* tocentospub@localhost identified by'wangyun';

                                  
Query OK, 0 rows affected (0.03 sec)

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

|+----------+
| user      |
+-----------+
| centospub |
+-----------+
1 row in set (0.02 sec)

mysql> exit;
Bye
[root@192 local]# mysql -u centospub -p; ← 用建立立的centospub使用者登入MySQL伺服器
Enter password:   ← 在這裡輸入密碼

 

mysql>create database test; ← 建立名為test的資料庫
mysql> show databases;       ← 查看系統已存在的資料庫

+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+
2 rows in set (0.01 sec)

mysql> use test ← 串連到資料庫
Database changed

mysql> create table test     
    -> (
    -> num int ,      ← 在資料庫中建立表
    -> name varchar(50)
    -> );
Query OK, 0 rows affected (0.14 sec)

mysql> show tables; ← 查看資料庫中已存在的表
+----------------+
| Tables_in_test |
+----------------+
| test           |
+----------------+
1 row in set (0.02 sec)

mysql> insert into test values(1,'Hello World!'); ← 插入一個值到表中
Query OK, 1 row affected (0.02 sec)

mysql> select * from test; ← 查看資料庫中的表的資訊
+------+-------------------+
| 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.14 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>  select *from test;  ← 查看資料庫中的表的資訊

+------+-----------------+
| num  | name            |
+------+-----------------+    ← 確認被更新到新的值
|    1 | Hello Everyone! |  
+------+-----------------+
1 row in set (0.01 sec)

mysql> delete from test where num=1; ← 刪除表內的值
Query OK, 1 row affected (0.01 sec)

mysql> select * from test; ← 確認刪除結果
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) ← 確認表已被刪除

mysql> drop database test; ← 刪除名為test的資料庫
Query OK, 0 rows affected (0.01 sec)

mysql> show databases; ← 查看已存在的資料庫
Empty set (0.01 sec)       

                  ← 確認test資料庫已被刪除(這裡非root使用者的關係,看不到名為mysql的資料庫)

mysql> exit ← 退出MySQL伺服器
Bye

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

 

[root@192 local]# mysql -u root -p;
Enter password:
mysql>  revoke all privileges on *.* from centospub@localhost;

                                            ← 取消centospub使用者對資料庫的操作許可權
Query OK, 0 rows affected (0.01 sec)

mysql> delete from mysql.user where user='centospub'and host='localhost';

                                                  ← 刪除centospub使用者
Query OK, 1 row affected (0.14 sec)

mysql> select user from mysql.user where user='centospub';

                        ← 尋找使用者centospub,確認已刪除與否
Empty set (0.01 sec)     ← 確認centospub使用者已不存在

mysql> flush privileges;  ← 重新整理,使以上操作生效
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

[root@192 local]# /etc/rc.d/init.d/httpd restart ← 重新啟動HTTP服務
                                                 ← 注意:語句後面無需;分號
停止 httpd:                                               [確定]
啟動 httpd:                                               [確定]

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.