1. 程式人生 > >Ubuntu下MySQL Server安裝

Ubuntu下MySQL Server安裝

        公司業務需要安裝MySQL,安裝過程比較簡單,但是在配置連線的中途遇到了不少坑,特此記錄下來。
        一、安裝:
              sudo apt-get update sudo apt-get install mysql-server
              sudo apt-get install mysql-client
              sudo apt-get install libmysqlclient-dev         

        二、配置:
              1、啟動 /etc/inint.d/mysql start
              2、停止 /etc/inint.d/mysql stop
              3、重啟 /etc/inint.d/mysql restart

        三、連線:

              在mysql server啟動後通過mysql -u(使用者名稱) -p(密碼)連線的時候出現了以下錯誤:
                    ERROR 2003 (HY000): Can't connect to MySQL server on '*.*.*.*' (111)
              出現這個問題是因為配置檔案中有bind_address=127.0.0.1 ,只能本地登入,需要修改my.cnf解決:
                    1、vim /etc/mysql/my.cnf
                    2、註釋掉bind-address = 127.0.0.1
                    3、/etc/init.d/mysql restart
              遮蔽掉之後客戶端再次連線又出現:
                    ERROR 1130 (HY000): Host '*.*.*.*' is not allowed to connect to this MySQL server
              這個問題是因為外部ip地址訪問資料庫並沒有許可權,需要讓資料庫給其分配許可權,在本地登入mysql,執行
                    1、設定指定ip能夠訪問mysql:
                                     GRANT ALL PRIVILEGES ON *.* TO 'user'@'hostip' IDENTIFIED BY 'password' WITH GRANT OPTION;
                          設定所有外部ip訪問mysql:
                                     GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
                    2、重新整理資料庫:
                          flush privileges;
                    3、退出資料庫並重啟服務:
                           exit;
                           /etc/inint.d/mysql restart
              修改之後客戶端再次連線又出現:
                    ERROR 1045 (28000): Access denied for user 'root'@'116.136.19.94' (using password: YES)
              一般這個錯誤是由密碼錯誤引起,可是這兒本地登入沒有問題,也就是不存在密碼錯誤的問題,其實是因為MySQL在外部ip強制在第一次登陸時修改root使用者的密碼,解決方案如下:
                    1、直接不使用密碼登入:
                            /etc/mysql/my.cnf 中[mysqld]欄位下新增
                            skip-grant-tables
                            這樣可以通過mysql -uroot -p 直接登陸沒有密碼
                    2、重置密碼:
                            use mysql;
                            desc user;//這裡設定的時候一定先看看欄位名字,下邊設定需要根據這個欄位設定,不同版本有區別
                            update user set Password=password('hadoop') where User='root';
                            update user set host='%' where User='root'
                            flush privileges;

              折騰了這麼幾輪,再次連線正常進入資料庫。