Linux下解決MySQL服務的兩個基本問題

來源:互聯網
上載者:User

使用mysql基本基本上會遇到主要的兩個問題.

1.第一次起動mysql是沒有問題的.對mysql做了一些操作,特別是刪除mysql中一些不要的帳號後,重新起動mysql會遇到這樣的問題
#/etc/init.d/mysqld restart
stopping mysql     [ok]
Timeout error occurred trying to start MySQL Daemon.  [failure]

 但是這個時候mysql實際上已經起動了,因為用netstat -ln命令去看3306連接埠已經起動.使用mysql -u root -p password也能串連到資料庫.
這實際上是mysql-3.x的一個bug(具體可以去看mysql的bugzilla和RedHat的bugzilla).
是什麼原因導致連線逾時呢?
我們不妨先看看/etc/init.d/mysqld起動指令碼是如何工作的,注意下面的一段

# If you've removed anonymous users, this line must be changed to
# use a user that is allowed to ping mysqld.
ping="/usr/bin/mysqladmin -uUNKNOWN_MYSQL_USER ping"
# Spin for a maximum of ten seconds waiting for the server to come up
        if [ $ret -eq 0 ]; then
            for x in 1 2 3 4 5 6 7 8 9 10; do
            if [ -n "`$ping 2> /dev/null`" ]; then
                    break;
            else
                    sleep 1;
            fi
            done

            if !([ -n "`$ping 2> /dev/null`" ]); then
                    echo "Timeout error occurred trying to start MySQL
Daemon."                    action $"Starting $prog: " /bin/false
            else
                    action $"Starting $prog: " /bin/true
            fi
        else
            action $"Starting $prog: " /bin/false
        fi
        [ $ret -eq 0 ] && touch /var/lock/subsys/mysqld
        return $ret

我們看到,指令碼判斷mysql是否起動,使用的是mysqladmin ping命令.

而這個命令想要正確執行是需要能夠登入mysql的.現在一些預設帳號已經刪除,而且其它帳號已經設定了密碼(預設沒有設定密碼).於是它沒有辦法串連到mysql.

不妨使用下面的命令測試一下
#mysqladmin -u root -ppassword ping
mysql alive

當你提供了帳號和密碼時,它的ping命令就可以正確執行了.
這個bug在mysql新出的mysql4.x可以解決.
但是RH9到FC3一直使用的是mysql3.x(不過mysql官方好象才推出mysql4.1,FC需要考慮問題性).
於是我用了下面的辦法臨時解決.
a)建立一個帳號,不設定密碼,不給任何許可權.
b)修改/etc/init.d/mysqld

下面我給出具體操作

#mysql -u root -p passwd
mysql>GRANT select ON test.* TO daemon@localhost

mysql>revoke select on test.* from daemon@localhost

開啟/etc/init.d/mysqld
把下面這行
ping="/usr/bin/mysqladmin -uUNKNOWN_MYSQL_USER ping"
修改為
ping="/usr/bin/mysqladmin -udaemon ping"
儲存,退出.

重新起動mysql

#/etc/init.d/mysqld restart
Stopping MySQL:                                            [  OK  ]
Starting MySQL:                                            [  OK  ]

如果你的第二行仍然是failure的話.再執行下面的命令
#/etc/init.d/mysqld start
這時應該式ok了.
如果這樣可以ok的話.
那麼你需要修改/etc/init.d/mysqld,
在restart函數的start後面再加一個start就可了.

2.即使剛安裝的mysql再起動後,去看日誌,給給出下面的這些資訊
Cannot initialize InnoDB as 'innodb_data_file_path' is not set.
If you do not want to use transactional InnoDB tables, add a line
skip-innodb
to the [mysqld] section of init parameters in your my.cnf
or my.ini. If you want to use InnoDB tables, add to the [mysqld]
section, for example,
innodb_data_file_path = ibdata1:10M:autoextend
But to get good performance you should adjust for your hardware
the InnoDB startup options listed in section 2 at
http://www.innodb.com/ibman.html
這是因為預設的資料庫起動指令碼需要載入innodb資料庫,但是mysql在做初始話時並沒有初始化時,並沒有載入這樣的資料庫.

因此這裡有兩種解決辦法:使用和不使用innodb.
我們先看不使用innodb的辦法.
其實這個方法就是跳過innodb的方法.
在/etc/my.cnf檔案的mysqld地區增加一行
skip-innodb就可以了.
如果我們需要使用innodb呢?
那麼可在/etc/my.cnf檔案的mysqld地區增加下面幾行
innodb_data_home_dir = /var/lib/mysql/
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /var/lib/mysql/
innodb_log_arch_dir = /var/lib/mysql/
set-variable = innodb_buffer_pool_size=16M
set-variable = innodb_additional_mem_pool_size=2M
set-variable = innodb_log_file_size=5M
set-variable = innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
set-variable = innodb_lock_wait_timeout=50
 儲存,退出.重啟起動mysql,再去看日誌.
應該不會再提示有關innodb的問題了.

相關文章

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.