1. 程式人生 > >CentOs中的Mysql配置

CentOs中的Mysql配置

用root使用者安裝完Mysql之後,指定了datadir為mysql使用者的主目錄,我這裡用mysql用來啟動Mysql服務。
在啟動之前,先用root使用者修改/etc/my.cnf檔案:

[mysqld]
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
log-error=/var/lib/mysql/log/mysqld.log
explicit_defaults_for_timestamp=true
  1. datadir目錄必須為空的,因為初始化要重新生成資料庫檔案。
  2. 每個目錄必須有讀寫許可權。
  3. explicit_defaults_for_timestamp=true
    配置是為了禁止生成下面的警告日誌:
2016-01-02T09:01:17.041119Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

然後啟動Mysql,啟動方式:
1. service mysqld start
2. /etc/init.d/mysqld start

第一種方式啟動,如果啟動使用者不是root,會有很多許可權問題。
我用第二種方式啟動,/etc/init.d/mysqld

是啟動指令碼,可以直接編輯。
其中,lockfile的預設配為

/var/lock/subsys/mysqld

mysql使用者對subsys目錄沒有寫的許可權,為了防止出現
touch: cannot touch '/var/lock/subsys/mysqld': Permission denied
的錯誤,這裡修改lockfile的目錄:

lockfile=/var/lib/mysql/mysqld

(如果有多個my.cnf檔案,想指定其中一個作為啟動引數檔案,修改/etc/init.d/mysqld檔案中的exec="/usr/bin/mysqld_safe",改為exec="/usr/bin/mysqld_safe --defaults-file=xxxx"

,xxxx為my.cnf路徑)。

開始啟動Mysql:

-bash-4.1$ /etc/init.d/mysqld start
touch: cannot touch `/var/log/mysqld.log': Permission denied
chown: cannot access `/var/log/mysqld.log': No such file or directory
chmod: cannot access `/var/log/mysqld.log': No such file or directory
Starting mysqld:  [  OK  ]

報錯了,果然不會讓我這麼快就成功。
BUT,我已經在/etc/my.cnf配置檔案中修改了log-error,為什麼還會出來這個原始的日誌檔案路徑?
恩,那就用root使用者建立/var/log/mysqld.log檔案,看看Mysql往裡面寫什麼東西,啟動成功,沒有報錯:

-bash-4.1$ /etc/init.d/mysqld start
Starting mysqld:  [  OK  ]

但是/var/log/mysqld.log檔案中什麼也沒有:

-rw-r-----  1 mysql mysql      0 Jan  2 17:01 mysqld.log

先這樣吧,一會看看是哪個日誌指向了這個路徑。

接著登入Mysql:

-bash-4.1$ mysql -uroot -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

記得以前用Mysql的時候,初始密碼都是空的,現在怎麼不行了?找不到密碼了。。。
搜尋解決方法,修改/etc/my.cnf,在[mysqld]下面增加一行:

skip-grant-tables

然後登入,成功:

-bash-4.1$ mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.10 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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> 

執行修改密碼命令:

mysql> update mysql.user a set a.authentication_string = password('root'), password_expired = 'N' where user = 'root' and host = 'localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> quit;

接下來刪除/etc/my.cnf配置檔案中的skip-grant-tables
然後正常登入:

-bash-4.1$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.10 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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>