1. 程式人生 > >mysql5.6 linux下安裝筆記

mysql5.6 linux下安裝筆記

由於現在要用mysql,以前一直用oracle ,現在下個mysql都要翻牆下載,官方網站都下不了,選擇之後都是windows版本。

最後找了個外國網站:http://fossies.org/linux/misc/mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz/ 下載了最新版本的:mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz

檢視當前centos版本和核心,位數。

[[email protected] ~]#  cat /etc/redhat-release --/etc/redhat-release配置檔案用一行內容來宣告 Red Hat 的名稱和版本號。 由 rc.local 使用.
CentOS release 6.5 (Final)  -- 發現我的是6.5版本。
[

[email protected] ~]# cat /proc/version   --64位系統
Linux version 2.6.32-431.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013
[[email protected] ~]# uname -a
Linux localhost.localdomain 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

一、把mysql壓縮包上傳到/usr/mysql 目錄下面。安裝目錄選擇/usr/local/mysql。

然後在網上找一篇安裝mysql的文章,最後參考了http://blog.csdn.net/zhanngle/article/details/41042631

-------------------------------------------

1.解壓tar包 cd /software tar -xzvf mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz  -- 更改對應的包名mysql-5.6.25-linux-glibc2.5-x86_64.tar.gz mv mysql-5.6.21-linux-glibc2.5-x86_64 mysql-5.6.21  --更改對應的安裝目錄/usr/local/mysql
2.新增使用者與組 groupadd mysql useradd -r -g mysql mysql chown -R mysql:mysql mysql-5.6.21 3.安裝資料庫 su mysql cd mysql-5.6.21/scripts ./mysql_install_db --user=mysql --basedir=/software/mysql-5.6.21 --datadir=/software/mysql-5.6.21/data (新的:./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data) exit 這裡可能會報錯:scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory。 需要安裝perl以及perl-devel.執行命令:yum –y install perl perl-devel 如果mysql資料路徑目前不是/usr/local/mysql/data,預設啟動會報下面的錯誤 starting MySQL...The server quit without updating PID file [FAILED]cal/mysql/data/iZwz93nu0s9j3y9zwzy
解決方法:在/etc/my.cnf配置檔案裡面,設定這一行:datadir = /other/data
配置好後重新啟動mysql 4.配置檔案 cd /software/mysql-5.6.21/support-files cp my-default.cnf /etc/my.cnf cp mysql.server /etc/init.d/mysql vim /etc/init.d/mysql          #若mysql的安裝目錄是/usr/local/mysql,則可省略此步 修改檔案中的兩個變更值 basedir=/software/mysql-5.6.21
datadir=/software/mysql-5.6.21/data
5.配置環境變數 vim /etc/profile export MYSQL_HOME="/software/mysql-5.6.21" export PATH="$PATH:$MYSQL_HOME/bin" 儲存退出 . /etc/profile 6.新增自啟動服務 chkconfig --add mysql chkconfig mysql on 7.啟動mysql service mysql start 8.登入mysql及改密碼與配置遠端訪問 進入mysql命令列之後修改root密碼: set password for [email protected] = password('123456');    -- 123456為新密碼
 mysql -uroot -p  --default-character-set=utf8     #登入mysql,需要輸入密碼 mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;     #允許root使用者遠端訪問 mysql>FLUSH PRIVILEGES;     #重新整理許可權 mysql>exit 9、Linux下mysql安裝完後是預設:區分表名的大小寫,不區分列名的大小寫;
用root帳號登入後,在/etc/my.cnf中的[mysqld]後新增新增lower_case_table_names=1,重啟MYSQL服務,這時已設定成功:不區分表名的大小寫;
lower_case_table_names引數詳解:
lower_case_table_names=0
其中0:區分大小寫,1:不區分大小寫

MySQL在Linux下資料庫名、表名、列名、別名大小寫規則是這樣的:
1、資料庫名與表名是嚴格區分大小寫的;
2、表的別名是嚴格區分大小寫的;
3、列名與列的別名在所有的情況下均是忽略大小寫的;
4、變數名也是嚴格區分大小寫的;
MySQL在Windows下都不區分大小寫。 10、修改預設字符集

預設情況下,mysql是不支援中文字元的。因為其預設的字符集latin1,我們可以通過修改其配置檔案,來更改預設的字符集設定。預設情況下的字符集如下:

mysql> SHOW VARIABLES LIKE '%char%'; +--------------------------+----------------------------+ | Variable_name            | Value                      | +--------------------------+----------------------------+ | character_set_client     | latin1                     | | character_set_connection | latin1                     | | character_set_database   | latin1                     | | character_set_filesystem | binary                     | | character_set_results    | latin1                     | | character_set_server     | latin1                     | | character_set_system     | utf8                       | | character_sets_dir       | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+

設定字符集為UTF-8, 編輯配置檔案/etc/my.cnf, 在[mysqld]下新增如下一行:default-character-set=utf8

http://skypegnu1.blog.51cto.com/8991766/1421777/      -- 發現有問題需要再驗證一下
---------------------------2014/11/25 新增 又裝了一次此版本的mysql,發現root使用者不能登入,提示沒許可權。使用mysqld_safe 登進去,發現user表居然是空的,沒有一個使用者,鬱悶。 於是從其它地方匯出了一份user表的sql語句,手動插入了一個使用者,再登入就可以了。 附: 忘記root密碼後,如何找回密碼 cd $MYSQL_HOME ./bin/mysqld_safe --basedir=/data/mysql-5.6.21 --datadir=/data/mysql-5.6.21/data --skip-grant-tables &
mysql -u root mysql
UPDATE user SET password=PASSWORD("new_password") WHERE user='root';
FLUSH PRIVILEGES;

二、按上面的文章安裝完之後

1、查詢當前安裝的mysql版本

檢視mysql是否啟動:service mysqld status 

[[email protected] ~]# mysql -V
mysql  Ver 14.14 Distrib 5.6.25, for linux-glibc2.5 (x86_64) using  EditLine wrapper

進入mysql、檢視mysql版本
方法一:status;
方法二:select version();

2、Mysql啟動、停止、重啟常用命令
a、啟動方式
1、使用 service 啟動:
[[email protected] /]# service mysqld start (5.0版本是mysqld)
[[email protected] etc]#
service mysql start (5.5.7版本是mysql)

2、使用 mysqld 指令碼啟動:
/etc/inint.d/mysqld start

3、使用 safe_mysqld 啟動:
safe_mysqld&

b、停止
1、使用 service 啟動:
service mysqld stop

2、使用 mysqld 指令碼啟動:
/etc/inint.d/mysqld stop

3、mysqladmin shutdown

c、重啟
1、使用 service 啟動:
service mysqld restart 
service mysql restart (5.5.7版本命令)

2、使用 mysqld 指令碼啟動:
/etc/init.d/mysqld restart

三、賦予許可權

登入: mysql -uroot -p  --default-character-set=utf8

(1)、建立使用者:

create user 'myuser'@'localhost' IDENTIFIED by '123456';

flush privileges;

(2)、建立資料庫:

create database mydb;

flush privileges;

(3)、給使用者授權資料庫相關許可權

grant all privileges on mydb.* to [email protected] identified by '123456';

flush privileges;

我用到的許可權命令,簡單說明,mydb為資料庫例項名,myuser為使用者名稱,後面對某個ip作對應的許可權控制。 
grant 為賦予許可權,revoke為撤銷許可權,賦予許可權完之後要用flush privileges; 來重新整理生效。

1、grant all on mydb.* to 'myuser'@'192.168.1.6'identified by '123456';
2、grant select on mydb.* to 'myuser'@'192.168.1.%' identified by '123456';
3、revoke all on mydb.*
from 'myuser'@'192.168.1.6' ;

grant all on mydb.* to 'myuser'@'localhost' identified by '123456';

授權之後,啟動應用。今天恢復資料庫之後發現一個問題,系統一直報錯:

java.sql.SQLException: Access denied for user 'myuser'@'127.0.0.1' (using password: YES)

還要配置一個127.0.0.1的。

grant all on mydb.* to 'myuser'@'127.0.0.1' identified by '123456';

檢視使用者許可權
show grants for 你的使用者
比如:
show grants for [email protected]’localhost’;

show grants for  'myuser'@'192.168.1.1';

檢視資料庫所有使用者
mysql> use mysql;
Database changed
mysql> select user,host from user;

刪除使用者   mysql> DROP USER 'myName'@'localhost';

     具體參考:http://www.cnblogs.com/llsun/archive/2013/08/06/3240963.html

四、匯入資料問題解決

ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...

1.執行批量的MYSQL語句,例如備份恢復的時候,時間過長; 或者SQL語句過大或者語句中含有BLOB類似欄位--我的是BLOB欄位存放檔案導致過大引起
對應:修改my.cnf中的wait_timeout和interactive_timeout變數 
在一些不便修改的情形下,如租用的空間,可以採取sql語句修改,如何做呢? 

(1)、如果是租用空間,無法修改資料庫引數,那麼臨時mysql語句設定,不過下次重新匯入必須再設定:
#對當前互動連結有效;  
mysql>set wait_timeout = someIntValue;  
#對後續起的互動連結有效;  
mysql>set interactive_timeout = someIntValue;  
mysql>set global max_allowed_packet=1000000000;  -- 設定針對BLOB欄位過大匯入引起的問題


(2)、如果是自己的伺服器,那麼找到my.ini(windows下my.ini)檔案,linux發現是/usr/local/mysql下的my.cnf檔案。

經測試恢復資料庫的時候報:ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER  privilege(s) for this operation
也是由於max_allowed_packet 設定太小引起,可以改到600M,重啟mysql,重新匯入就不報這個問題了,參考網上發現授權也不能解決這個問題,後面還是修改這個引數值來解決。

新增一句max_allowed_packet=16M,如果不行將16M再加大
加大wait_timeout也可起一定作用
eg:
wait_timeout=2880000
interactive_timeout = 2880000
max_allowed_packet = 600M

應用時記住重啟資料庫哦~~
除此之外,可能還有一些別的buffer_size的變數會影響到,也值得注意 
例如 
read_buffer_size 
read_rnd_buffer_size

重啟後查詢

mysql> show variables like '%timeout';
+-----------------------------+----------+
| Variable_name               | Value    |
+-----------------------------+----------+
| connect_timeout             | 10       |
| delayed_insert_timeout      | 300      |
| innodb_flush_log_at_timeout | 1        |
| innodb_lock_wait_timeout    | 50       |
| innodb_rollback_on_timeout  | OFF      |
| interactive_timeout         |
2880000  |
| lock_wait_timeout           | 31536000 |
| net_read_timeout            | 30       |
| net_write_timeout           | 60       |
| rpl_stop_slave_timeout      | 31536000 |
| slave_net_timeout           | 3600     |
| wait_timeout                |
2880000  |
+-----------------------------+----------+
12 rows in set (0.00 sec)

mysql> show VARIABLES like '%max_allowed_packet%';
+--------------------------+------------+
| Variable_name            | Value      |
+--------------------------+------------+
| max_allowed_packet       | 104857600  |
| slave_max_allowed_packet | 1073741824 |
+--------------------------+------------+
2 rows in set (0.00 sec)

五、安裝完之後訪問參考

http://blog.csdn.net/cuker919/article/details/44997923

六、啟動應用

啟動應用訪問發現應用日誌連線池報:java.sql.SQLException: Access denied for user 'myuser'@'localhost' (using password: YES)

同時切換到linux,用該使用者試試:

[[email protected] ]# mysql -u myuser-p
Enter password:
ERROR 1045 (28000): Access denied for user 'myuser'@'localhost' (using password:

但發現mysql workbench用該使用者訪問一切正常,因為我是用mysql workbench建立的使用者,但到linux又登入不了。

下面是試錯的過程,但既然後面可以登入了

(1)、修改密碼

[[email protected] ]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 614
Server version: 5.6.25 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> 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 password=password("123456") where user="root";  --更改root使用者密碼
Query OK, 3 rows affected (0.00 sec)
Rows matched: 5  Changed: 3  Warnings: 0

mysql> update user set password=password("123456") where user="myuser"; --更改myuser使用者密碼
Query OK, 4 rows affected (0.00 sec)
Rows matched: 5  Changed: 4  Warnings: 0

發現用這個命令修改密碼提示:ERROR 1146 (42S02): Table 'mydb.user' doesn't exist

參考:http://www.cnblogs.com/fly1988happy/archive/2011/12/15/2288554.html

update mysql.user set password=password('新密碼') where User="test" and Host="localhost";  -- 最後這樣更改才ok

mysql> exit  --退出

(2)、再登入,還是報錯

[[email protected] logs]# mysql -u myuser-p
Enter password:
ERROR 1045 (28000): Access denied for user 'myuser'@'localhost' (using password: 

(3)、再試錯。

[[email protected] ]# /etc/init.d/mysql stop  --停止服務
Shutting down MySQL..... SUCCESS!
[[email protected] ]# /usr/bin/mysqld_safe --skip-grant-tables
-bash: /usr/bin/mysqld_safe: No such file or directory
[[email protected] ]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[1] 1791
[[email protected] ]# 150613 11:20:01 mysqld_safe Logging to '/usr/local/mysql/                                                                                                                               data/localhost.localdomain.err'.
150613 11:20:02 mysqld_safe Starting mysqld daemon with databases from /usr/loca                                                                                                                               l/mysql/data
^C  --按ctrl + c 退出
[[email protected] ]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking
150613 11:21:27 mysqld_safe Logging to '/usr/local/mysql/data/localhost.localdom                                                                                                                               ain.err'.
150613 11:21:27 mysqld_safe A mysqld process already exists
[[email protected] ]# /etc/rc.d/init.d/mysql restart  --啟動服務

[[email protected] ]# mysql -u myuser-p  -- 再登入,既然可以了,然後應用也可以登入

(4)、總結

先登入 mysql

# mysql -u root -p

mysql> use mysql   --切換

mysql> select host,user,Password from user;  --檢視使用者和密碼情況,其實從這裡就可以發現問題,不行就往下

# /etc/init.d/mysql stop
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
# mysql -u root -p
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES;
mysql> quit
# /etc/init.d/mysqld restart
# mysql -u myuser -p

參考:http://www.bxl.me/zh-cn/2567.html  
http://blog.csdn.net/lyflower/article/details/6137021 
http://blog.csdn.net/vurtne_ye/article/details/26514499

七、mysql檢視所有儲存過程,函式,檢視,觸發器,表

方法一:
       select `name` from mysql.proc where db = 'your_db_name' and `type` = 'PROCEDURE'   //儲存過程
       select `name` from mysql.proc where db = 'your_db_name' and `type` = 'FUNCTION'   //函式

方法二:
         show procedure status; //儲存過程
        show function status;     //函式

檢視儲存過程或函式的建立程式碼
show create procedure proc_name;
show create function func_name;

檢視檢視
SELECT * from information_schema.VIEWS   //檢視
SELECT * from information_schema.TABLES   //表

檢視觸發器
方法一:
語法:SHOW TRIGGERS [FROM db_name] [LIKE expr]
例項:SHOW TRIGGERS\G     //觸發器

方法二:
對INFORMATION_SCHEMA資料庫中的TRIGGERS表查詢
mysql>SELECT * FROM triggers T WHERE trigger_name=”mytrigger” \G