centOS安裝mysql---glibc方式

來源:互聯網
上載者:User

標籤:rom   body   groupadd   local   get   成功   root密碼   工具   inux   

寫在前面:

   首先,centos是自己整合mysql的。但是我要用的伺服器人家沒給裝。

   其次,centos是可以yum安裝mysql的,我很高興而且輕鬆的用yum把mysql安裝上了。但是,啟動並執行時候很沮喪。yum安裝的mysql,當再裝SQLAlchemy的時候,報了滿屏的錯誤。我看著滿屏的各種錯無從下手。本來mysql就不熟。

   最後,只好去mysql的網站,down下來源碼包,一點一點的安裝下來。整整費了我一天。下面我把這的過程記錄下來,以後用就方便了。

 

下載mysql:

http://downloads.mysql.com/archives.php

選擇一個mysql的版本,之後一定要看好,下glibc的。如:mysql-5.0.90-linux-i686-glibc23.tar.gz

本例中下載到了/media目錄下,這個不是好習慣...

 

▲安裝mysql:

下面是linux命令

[plain] view plain copy 
  1. :$ sudo groupadd mysql  
  2. :$ sudo useradd -g mysql mysql  
  3. :$ cd /usr/local  
  4. :$ tar zvxf /media/mysql-5.0.90-linux-i686-glibc23.tar.gz    
[plain] view plain copy 
  1. :$ mv mysql-5.0.90-linux-i686-glibc23 mysql     
[plain] view plain copy 
  1. :$ cd mysql  
  2. :$ sudo chown -R mysql .  
  3. :$ sudo chgrp -R mysql .  
  4. :$ scripts/mysql_install_db --user=mysql  --basedir=/usr/local/mysql  
  5. :& cd ..  
  6. :$ sudo chown -R root mysql .  
  7. :$ cd mysql  
  8. :$ sudo chown -R mysql data  
  9. :$ bin/mysqld_safe --basedir=/usr/local/mysql --user=mysql &  

 

至此,mysql安裝成功。

因為在運行狀態,我沒有ctrl-c,只好再開一個ssh視窗...

 

▲為mysql的root使用者添加密碼

下面是linux命令

 

[c-sharp] view plain copy 
  1. cd /usr/local/mysql/bin ./mysql -u root  

 

進入mysql後:

 

[c-sharp] view plain copy 
  1. mysql>  GRANT ALL PRIVILEGES ON *.* TO [email protected] IDENTIFIED BY "chang";  

 

其實是設定了root的localhost的密碼為chang
顯示執行成功,然後exit退出mysql。

之後,再次登入mysql,這次要用密碼了:

 

[c-sharp] view plain copy 
  1. cd /usr/local/mysql/bin./mysql -u root -p  

 

輸入密碼chang之後,可以正常登入,如下:

Welcome to the MySQL monitor.  Commands end with ; or /g.
Your MySQL connection id is 1
Server version: 5.0.90 MySQL Community Server (GPL)

Type ‘help;‘ or ‘/h‘ for help. Type ‘/c‘ to clear the current input statement.

 

查看一下使用者資訊:

 

[c-sharp] view plain copy 
  1. mysql> select user,host,password from mysql.user;  

 

結果如下:

+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |  *F05D019BA3BEC01CA9FBD4141E4EA57A28EF3EDF  |   ← (root密碼為chang) 
| root | linux        |            |   ← (root密碼為空白) 
| root | 127.0.0.1 |           |   ← (root密碼為空白) 
|        | localhost  |           | 
+------+-----------+----------+

分別更改它們的密碼:

 

[c-sharp] view plain copy 
  1. mysql> set password for [email protected]=password(‘chang‘);  

 

 

[c-sharp] view plain copy 
  1. mysql> set password for [email protected]=password(‘chang‘);   

 

 

[c-sharp] view plain copy 
  1. mysql> set password for [email protected]=password(‘chang‘);   

 

再次查看使用者資訊會發現已經更改過來。

然後退出mysql。

 

▲把mysql做成服務

 

[c-sharp] view plain copy 
  1. sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql   

 

啟動mysql服務

 

[c-sharp] view plain copy 
  1. sudo /etc/init.d/mysql start  

 

這時候就可以重啟機器試試了
重啟後再登陸mysql,發現可以登陸。服務製作成功!

 

▲配置mysql

 

[c-sharp] view plain copy 
  1. vi /etc/my.cnf  

 

(注釋:如果沒有自動產生my.cnf檔案,那麼:安裝完的mysql包下有個support-files檔案夾,其中有my-huge.cnf等,將my-huge copy一份,改名為my.cnf,將其適當地修改(當然是根據你的資料庫配置)然後copy至/etc/my.cnf)

開啟my.cnf後

找到[client] 添加: 
default-character-set = utf8      # 預設字元集為utf8

找到[mysqld] 添加:
default-character-set = utf8       #預設字元集為utf8
init_connect = ‘SET NAMES utf8‘ #設定串連mysql資料庫時使用utf8編碼,以讓mysql資料庫為utf8運行

 

修改好後,重新啟動即可.
我這裡是重啟了mysql的服務:

 

[c-sharp] view plain copy 
  1. sudo /etc/init.d/mysql restart   


(有一次找不到sock,這樣重啟兩次服務之後居然可以了!!!汗。)

 

 

之後進入mysql,查一下是否更改了字元集:

[c-sharp] view plain copy 
  1. cd /usr/local/mysql/bin./mysql -u root -p  

 

 

[c-sharp] view plain copy 
  1. mysql> show variables like ‘character%‘;   

 

出現下面的畫面:

+--------------------------+----------------------------------------+
| Variable_name            | Value                                  |
+--------------------------+----------------------------------------+
| character_set_client     | utf8                                   | 
| character_set_connection | utf8                                   | 
| character_set_database   | utf8                                   | 
| character_set_filesystem | binary                                 | 
| character_set_results    | utf8                                   | 
| character_set_server     | utf8                                   | 
| character_set_system     | utf8                                   | 
| character_sets_dir       | /usr/local/mysql/share/mysql/charsets/ | 
+--------------------------+----------------------------------------+

好,資料庫語言完畢。

 

▲開啟mysql的遠端存取
mysql預設是不允許遠端存取的。
用密碼登陸mysql,可以正常登陸,但是換台機器用工具連,就報錯:
ERROR 1130: Host 192.168.1.6 is not allowed to connect to this MySQL server

 

方法: 改表法。mysql預設是不允許遠端存取的,只能在localhost訪問。這個時候只要在localhost的那台電腦,登入mysql後,更改 “mysql” 資料庫裡的 “user” 表裡的 “host” 項,從”localhost”改成”%”

mysql -u root -p
Enter password: chang
mysql>use mysql;
mysql>update user set host = ‘%‘ where user = ‘root‘;  //可能會報錯
mysql>flush privileges; 
mysql>select host,user from user where user=‘root‘;
  

執行完上面的,就可以遠端連線了!

注釋:

update user set host = ‘%‘ where user = ‘root‘; //這個命令執行錯誤時,可能會報錯:
ERROR 1062 (23000): Duplicate entry ‘%-root‘ for key 1;
解決方案:
1,不用管它。呵呵。
2,改成這樣執行
update user set host=‘%‘ where user=‘root‘ and host=‘localhost‘;
也就是把localhost改成了所有主機。

 

 ---------------------------------------------------
之後運行app程式,報錯:
ImportError: libmysqlclient_r.so.15: cannot open shared object file: No such file or directory

解決辦法是把/usr/local/mysql/lib下的
libmysqlclient_r.so.15
拷貝到/usr/lib解決。

 

至此,mysql安裝配置完畢!

 

 

 ================= 我是華麗的分割線 ========================

----------------------------------------------------
■附註:
附註1: 重啟和關閉mysql服務
重啟mysql服務
:$ sudo /etc/init.d/mysql restart
關閉mysql服務
:$ sudo /etc/init.d/mysql stop 
----------------------------------------------------
附註2: 非服務狀態下,啟動和停止mysql
啟動mysql
代碼: 
:& cd /usr/local/mysql
:& bin/mysqld_safe --basedir=/usr/local/mysql --user=mysql &

停止mysql
代碼: 
:& cd /usr/local/mysql
:$ bin/mysqladmin -uroot -ppassw0rd shutdown

----------------------------------------------------
附註3: mysql命令列中文顯示?號
mysql> set names utf8;

---------------------------------------------------
附註4: mysql的資料庫存放路徑
/var/lib/mysql

---------------------------------------------------
附註5: 從mysql中匯出和匯入資料
mysqldump 資料庫名 > 檔案名稱 #匯出資料庫
mysqladmin create 資料庫名 #建立資料庫
mysql 資料庫名 < 檔案名稱 #匯入資料庫

---------------------------------------------------
附註6: 修改mysql的root口令
sudo mysqladmin -u root -p password ‘你的新密碼‘

或者:括弧裡是新密碼
use mysql; 
update user set Password=password(‘chang‘) where User=‘root‘; 
flush privileges;

---------------------------------------------------
附註7: 忘了mysql的root口令怎麼辦
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
sudo mysqladmin -u user password ‘newpassword
sudo mysqladmin flush-privileges

---------------------------------------------------
附註8: 輸入要登入的mysql主機
./mysql -u root -h 127.0.0.1 -p

centOS安裝mysql---glibc方式

聯繫我們

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