一 安裝環境node
(1)CentOS版本:CentOS-7mysql
查看方法:linux
[root@bogon 桌面]# cat /etc/redhat-releasesql
CentOS Linux release 7.0.1406 (Core)數據庫
來源:官網下載centos
下載地址:http://www.centos.org/服務器
(2)MySQL版本:MySQL-5.6.22oracle
來源:官網下載工具
下載地址:https://edelivery.oracle.com/EPD/Search/handle_go測試
或者http://pan.baidu.com/s/1dDu6n9R
名稱:MySQL Database 5.6.22 RPM for Oracle Linux / RHEL 7 x86 (64bit)
在網上搜了一下,Linux下安裝MYSQL有三種方式:
1 經過yum命令在線下載安裝
yum install mysql-devel
2 下載離線rpm安裝包安裝
3 下載源碼編譯安裝
方式1不打算用,由於咱們大部分項目服務器是不能直接上Internet的,網上關於在線安裝的教程不少,方式3對於只要應用MYSQL的人來講不必畫蛇添足。
1. 解壓下載的zip包,會發現有如下幾個rpm包:
MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-embedded-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-shared-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-shared-compat-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-test-advanced-5.6.22-1.el7.x86_64.rpm
2. 卸載MariaDB
若是直接點擊rpm包安裝會獲得錯誤提示。由於CentOS的默認數據庫已經再也不是MySQL了,而是MariaDB,爲何呢?
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可。開發這個分支的緣由之一是:甲骨文公司收購了MySQL後,有將MySQL閉源的潛在風險,所以社區採用分支的方式來避開這個風險。MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。
查看當前安裝的mariadb包:
[root@bogon 桌面]# rpm -qa | grep mariadb
將它們通通強制性卸載掉:
[root@bogon 桌面]# rpm -e --nodeps mariadb-libs-5.5.35-3.el7.x86_64
[root@bogon 桌面]# rpm -e --nodeps mariadb-5.5.35-3.el7.x86_64
[root@bogon 桌面]# rpm -e --nodeps mariadb-server-5.5.35-3.el7.x86_64
3. 安裝MYSQL
雙擊下面三個包進行自動安裝:
MySQL-client-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-devel-advanced-5.6.22-1.el7.x86_64.rpm
MySQL-server-advanced-5.6.22-1.el7.x86_64.rpm
提示:其實第二個包devel我也不知道是幹什麼的,也不知道是否是必須的(上網搜了一下應該不是必須的),沒有測試是否必須就已經點來裝上了,也不想花時間去測試是否必須了,有測試過的朋友麻煩留言告知。
4. 啓動MYSQL
[root@bogon 桌面]#service mysql start
獲得錯誤:ERROR!The server quit without updating PID file
咱們這裏主要是由於:selinux惹的禍,若是是centos系統,默認會開啓selinux。解決方法是關閉它,打開/etc/selinux/config,把SELINUX=enforcing改成SELINUX=disabled後存盤退出重啓機器。
而後再啓動mysql就沒問題了:
[root@bogon 桌面]#service mysql start
查看MySQL運行狀態:
[root@bogon 桌面]# service mysql status
SUCCESS! MySQL running (2377)
5. 默認root用戶登陸MYSQL
[root@bogon 桌面]# mysql -u root -p
Enter password:
ERROR 1045 (28000):Access denied for user 'root'@'localhost' (using password: YES)
發現有有錯誤,而後在網上查了一下說使用下面命令修改root初始化密碼:
[root@bogon 桌面]# /usr/bin/mysqladmin -u root password 'passok'
/usr/bin/mysqladmin: connect to server at'localhost' failed
error: 'Accessdenied for user 'root'@'localhost' (using password: NO)'
發現MYSQL數據庫默認的root用戶仍是沒辦法設置密碼進行登陸,須要作一下操做:
最近新裝好的mysql在進入mysql工具時,老是有錯誤提示:
# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
使用網上介紹的方法修改root用戶的密碼:
# mysqladmin -uroot -p password 'newpassword'
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
如今終於被我找到了解決方法,以下(請先測試方法三,謝謝!):
方法一:
# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root mysql
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
重置MySQL中root用戶密碼及驗證
仍是不行,而後在網上又找到一個重置MySQL中root用戶密碼及驗證的方法:
(1) 中止MySQL服務
[root@bogon 桌面]# service mysql stop
Shutting down MySQL.. SUCCESS!
(2) 輸入繞過密碼認證命令
[root@bogon 桌面]# mysqld_safe --user=mysql --skip-grant-tables--skip-networking &
執行上面命令後,此會話窗口會出現無反應的狀態,須要使用CTRL+C中斷會話,檢查/var/lib/mysql/DB-Server.err日誌,發現也無其它異常信息。沒有弄明白該會話窗口爲啥會出現無響應狀態。
接着
mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> select Host, User, Password,password_expired
-> from user
-> where user='root' and host='root' or host='localhost';
+-----------+------+-------------------------------------------+------------------+
| Host | User | Password | password_expired |
+-----------+------+-------------------------------------------+------------------+
| localhost | root | *A848DE7CCD839E924921BEE41711991DDA0D529E | Y |
+-----------+------+-------------------------------------------+------------------+
1 row in set (0.00 sec)
mysql> update user set password=PASSWORD('p12#456')
-> where user='root' and host='root' or host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
新設置用戶或更改密碼後需用flush privileges刷新MySQL的系統權限相關表,不然會出現拒絕訪問,還有一種方法,就是從新啓動mysql服務器,來使新設置生效。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@DB-Server init.d]# /etc/rc.d/init.d/mysql restart
Shutting down MySQL..140722 15:02:27 mysqld_safe mysqld from pid file /var/lib/mysql/DB-Server.pid ended
[ OK ]
Starting MySQL.[ OK ]
[2]- Done mysqld_safe --user=mysql --skip-grant-tables --skip-networking
[root@DB-Server init.d]# mysql -u root p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@DB-Server init.d]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.19
Copyright (c) 2000, 2014, 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數據庫後執行腳本遭遇 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement,能夠使用從新設置一次密碼便可解決問題.
mysql>set password = password('p12#456');