今天,新拿了一臺服務器,準備作測試服務器,通常來講,簡單的使用yum安裝mysql就能夠用了。mysql
但這臺比較特殊。 linux
這臺服務器上原來安裝過mysql,可是啓動不起來了,啓動的時候報錯,具體什麼錯誤,沒有具體去分析解決了,怕花費的時間太長,耽誤環境部署。 因而決定重裝mysql sql
把原有的mysql卸載掉,把相關文件所有刪掉,使用如下命令安裝數據庫
yum -y install mysql mysql-server mysql-develbash
安裝過程沒有問題,但就是啓動不起來,仍是報錯服務器
因而換一下思路,用rpm包來安裝socket
但這樣就致使一個問題ide
老是會提示文件跟以前的有衝突,但實際上文件系統已經不存在那些文件了。 測試
爲了避免在這個問題上面糾結,我再換種方式,使用官網提供的二進制程序進行安裝和配置ui
過程以下:
1.下載tar包 (如下mysql 均以5.7版本爲例)
https://dev.mysql.com/downloads/mysql/
選擇linux -generic 版本 選擇匹配位數的程序進行下載
2.解壓並放置到/usr/local中
tar xf xxx.tar -C /usr/local 解出xxx.tar.gz的包
tar -zxvf xxx.tar.gz 再解壓一次,解壓出mysql程序
mv xxx mysql 將目錄名重置爲mysql
3.配置
由於本機已經設置好了/etc/my.cnf 因此,這裏不作my.cnf的配置說明
複製程序
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
添加mysqld服務
chkconfig --add mysqld
開啓mysql服務
chkconfig mysqld on
4.設置權限,前提是已經設置好了mysql用戶組,以及mysql用戶
要對mysql文件夾和mysql的數據文件夾進行權限設置
好比數據存放在 /home/data
那麼須要執行
chown -R mysql:mysql /home/data
chown -R mysql:mysql /usr/local/mysql/
若是還有報錯,請給一下目錄受權
日誌目錄 /var/log
pid文件所在目錄 ,若是提示找不到pid文件,先確認路徑,路徑無誤的話,能夠自建一個 主機名.pid文件,來代替。
5.執行安裝腳本。 本人親測,在以上這些事情都作完以後,執行service mysqld start命令時,出現如下錯誤
ERROR! The server quit without updating PID file (/usr/local/mysql/c.pid).
由於這個錯誤出現過不少次了,我直接去看日誌文件內容,發現有這樣一句話
mysqld: Table 'mysql.plugin' doesn't exist
我回過頭看安裝過程,發現少作了一件事情,執行mysql_install_db這個腳本,因而就去執行該腳本。
[root@c bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/data
2017-01-08 01:13:17 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2017-01-08 01:13:17 [ERROR] The data directory '/home/data' already exist and is not empty.
看到這裏,把/home/data清理如下,清除全部文件。再執行。
仍是有警告,可是執行成功了。因而遵循這個警告,去執行
[root@c bin]# ./mysqld --initialize
2017-01-07T17:17:18.084687Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-07T17:17:18.088298Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2017-01-07T17:17:18.088332Z 0 [ERROR] Aborting
這裏的error是由於我當時配置文件中數據目錄(datadir)寫錯了致使的,改成正常使用的目錄,就沒問題。
再改進 ,並執行
[root@c bin]# ./mysqld --initialize --explicit_defaults_for_timestamp
2017-01-07T17:25:23.122239Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-01-07T17:25:23.524486Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-01-07T17:25:23.738581Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 45b0f5ce-d4fe-11e6-830f-f4ce46b3a1d8.
2017-01-07T17:25:23.779377Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2017-01-07T17:25:23.780359Z 1 [Note] A temporary password is generated for root@localhost: Ju<+)oguk1aA
2017-01-07T17:25:39.770568Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770631Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770666Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770689Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
2017-01-07T17:25:39.770755Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
咱們能夠看到,出現了一些警告,而且同時隨機生成了一個密碼。在這個過程當中,原本想加--inital-insecure這個參數試一下,結果發現不能識別這個參數。
[root@c bin]# ./mysqld --initialize --explicit_defaults_for_timestamp --inital-insecure
2017-01-07T17:24:22.109446Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-01-07T17:24:22.539774Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-01-07T17:24:22.738196Z 0 [ERROR] unknown option '--inital-insecure'
2017-01-07T17:24:22.738223Z 0 [ERROR] Aborting
這時候,咱們試一下 service mysqld start
[root@c bin]# service mysqld start
Starting MySQL. SUCCESS!
查看一下mysql進程
[root@c support-files]# ps aux|grep mysql
root 4995 0.0 0.0 11440 1532 pts/0 S 01:26 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/home/data --pid-file=/home/data/c.pid
mysql 5188 0.0 0.7 1296856 189552 pts/0 Sl 01:26 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/home/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld.log --pid-file=/home/data/c.pid --socket=/var/lib/mysql/mysql.sock
root 5246 0.0 0.0 103324 900 pts/0 S+ 02:06 0:00 grep mysql
終於成功搞定了。接下來試下登錄
[root@c support-files]# mysql -uroot -p
-bash: mysql: command not found
沒有mysql命令? 怎麼辦?
linux系統默認會從/usr/bin 來查找命令,因此咱們能夠經過如下方法來解決
[root@c support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin
再接着登錄
[root@c support-files]# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
又報錯了。錯誤的緣由在於路徑/tmp/mysql.sock不存在
解決方法
找到mysql.sock路徑,默認是:/var/lib/mysql/mysql.sock,創建關係
[root@c /]# ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
接下再登錄
[root@c /]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
出現了一個常見錯誤
解決此錯誤,分爲三步
1.中止服務
[root@c /]# service mysqld stop
2.修改密碼
[root@c /]# cd /usr/local/mysql/bin/
[root@c bin]# ./mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[root@c bin]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string=Password('admin123.0') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3.重啓服務再登錄
關閉mysql相關的進程,而後執行
[root@c bin]# service mysqld start
Starting MySQL. SUCCESS!
再次登錄
[root@c bin]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.17
Copyright (c) 2000, 2016, 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安裝和使用的問題,都解決了,接下來就能夠愉快的使用mysql數據庫作業務設計了。
但願個人筆錄也能幫到您,感謝您的閱讀。