Mysql安裝步驟:html
系統版本:mysql
[root@db01 ~]# cat /etc/redhat-release linux
CentOS release 6.8 (Final)web
一、上傳MySQL二進制安裝包:sql
https://pan.baidu.com/disk/home#category/type=6&vmode=list ###-網盤下載地址數據庫
[root@db01 ~]# cd /home/oldboy/tools/ [root@db01 tools]# rz -y [root@db01 tools]# ll -h 總用量 178M-rw-r--r-- 1 root root 178M 5月 16 2016 mysql-5.5.49-linux2.6-x86_64.tar.gz
二、檢查當前系統中是否存在mysql用戶,若是不存在就新建mysql用戶,而後解壓縮mysql的安裝包:安全
[root@db01 tools]# useradd -s /sbin/nologin -M mysql [root@db01 tools]# id mysql uid=501(mysql) gid=501(mysql) 組=501(mysql) [root@db01 tools]# tar xf mysql-5.5.49-linux2.6-x86_64.tar.gz
# 因爲是二進制安裝包,不須要進行make和make installapp
# 只要把解壓事後的目錄移動到/application/下便可ide
[root@db01 tools]# mkdir -p /application [root@db01 tools]# mv mysql-5.5.49-linux2.6-x86_64 /application/mysql-5.5.49 [root@db01 tools]# cd /application/ [root@db01 application]# ln -s /application/mysql-5.5.49/ /application/mysql [root@db01 application]# ls -l 總用量 8l rwxrwxrwx 1 root root 26 5月 30 23:11 mysql -> /application/mysql-5.5.49/ drwxr-xr-x 13 root root 4096 5月 30 23:09 mysql-5.5.49
# 受權mysql管理數據庫文件學習
# 受權究竟是在初始化後仍是在初始化前,須要進行測試
[root@db01 application]# chown -R mysql.mysql /application/mysql/
三、初始化mysql數據庫
# 初始化過程當中出現2個OK就表示成功,出現error要解決,出現warnnig能夠忽略
# 若是初始化失敗,須要刪除/application/mysql/data/目錄下全部數據文件
# 以後解決報錯,再次初始化
# 若是/tmp目錄權限不對,也會致使mysql初始化失敗,/tmp目錄權限爲1777
[root@db01 application]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql Installing MySQL system tables...OK Fillinghelp tables...OK Tostart mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASEREMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! Todo so, start the server, then issue the following commands:
四、生成mysql配置文件
# mysql配置文件位置
# 通常虛擬機使用my-medium.cnf和my-small.cnf
# 學習參數使用my-innodb-heavy-4G.cnf
[root@db01 application]# cd /application/mysql/support-files/ [root@db01 support-files]# ll 總用量 96-rw-r--r-- 1 mysql mysql 4691 Jun 19 2013 my-huge.cnf # 巨大配置 -rw-r--r-- 1 mysql mysql 19759 Jun 19 2013 my-innodb-heavy-4G.cnf # 重大配置 -rw-r--r-- 1 mysql mysql 4665 Jun 19 2013 my-large.cnf # 比較大配置 -rw-r--r-- 1 mysql mysql 4676 Jun 19 2013 my-medium.cnf # 最小配置 -rw-r--r-- 1 mysql mysql 2840 Jun 19 2013 my-small.cnf # 比較小配置
# 覆蓋系統自帶的mysql配置文件
[root@db01 support-files]# cp my-small.cnf /etc/my.cnf cp:是否覆蓋"/etc/my.cnf"? y
五、更改mysql默認啓動文件裏的目錄位置
因爲是咱們自定義mysql二進制包安裝路徑在/application/mysql目錄下,可是,二進制默認的安裝路徑是/usr/local/mysql,這能夠經過mysql啓動文件看出來:
那麼咱們須要將該啓動文件裏的/usr/local/mysql變成/application/mysql目錄。
[root@db01 support-files]# sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe
以後,後臺啓動mysql
[root@db01 support-files]# /application/mysql/bin/mysqld_safe & [1] 1944 15102109:37:28 mysqld_safe Logging to ‘/application/mysql/data/db01.err‘. 15102109:37:28 mysqld_safe Starting mysqld daemon with databases from/application/mysql/data
# 按下回車鍵
檢查mysql的3306端口:
[root@db01 support-files]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEmysqld 2162 mysql 10u IPv4 18922 0t0 TCP *:mysql (LISTEN)
六、配置環境變量
[root@db01 support-files]# tail -1 /etc/profile export PATH=/application/mysql/bin:$PATH [root@db01 support-files]# source /etc/profile [root@db01 support-files]# which mysql /application/mysql/bin/mysql
七、登陸mysql
[root@db01 support-files]# mysql Welcome to the MySQL monitor. Commands end with ; or \g...mysql> exit Bye
八、安裝過程的排錯
# mysql的錯誤日誌,在/application/mysql/data目錄下以主機名命名的err文件
[root@db01 data]# pwd /application/mysql/data [root@db01 data]# ll總用量 28692-rw-r----- 1 mysql root 1995 5月 29 19:15 db01.err
九、配置科學啓動mysql
[root@db01 support-files]# pwd /application/mysql/support-files
# mysql.server是一個啓動腳本
[root@db01 support-files]# cp mysql.server /etc/init.d/mysqld
# 再次改變mysql的默認路徑
[root@db01 support-files]# sed -i ‘s#/usr/local/mysql#/application/mysql#g‘/etc/init.d/mysqld
# 增長執行權限
[root@db01 support-files]# chmod +x /etc/init.d/mysqld
# 中止以前啓動的mysql,直到提示「mysqld: no process killed」
# 使用killall中止mysql,生產環境不要使用pkill中止mysql,影響太大
[root@db01 support-files]# killall mysqld 151021 10:01:47 mysqld_safe mysqld from pid file/application/mysql/data/db01.pid ended [1]+ Done /application/mysql/bin/mysqld_safe [root@db01 support-files]# [root@db01 support-files]# killall mysqld mysqld:no process killed
# 肯定mysql已經中止服務
[root@db01 support-files]# lsof -i :3306 [root@db01 support-files]#
# 從新啓動mysql服務
[root@db01 support-files]# /etc/init.d/mysqld startStartingMySQL.. SUCCESS! [root@db01 support-files]#
# 檢查端口
[root@db01 support-files]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 2872 mysql 10u IPv4 23529 0t0 TCP *:mysql (LISTEN) [root@db01 support-files]#
# 配置開機自啓動mysql,也能夠放在rc.local裏
[root@db01 support-files]# chkconfig mysqld on [root@db01 support-files]# [root@db01 support-files]# chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@db01 support-files]#
# 再次登陸mysql
[root@db01 support-files]# mysql Welcometo the MySQL monitor. Commands end with; or \g... mysql>
十、配置mysql登陸密碼
# 初始化時顯示的設置密碼和修改密碼語句
/application/mysql/bin/mysqladmin-u root password ‘new-password‘ /application/mysql/bin/mysqladmin-u root -h db01 password ‘new-password‘
# 設置密碼
[root@db01 support-files]# mysqladmin -uroot password 123456 [root@db01 support-files]#
# 不用密碼登陸報錯
[root@db01 support-files]# mysql ERROR1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO) [root@db01 support-files]#
# 使用用戶名和密碼登陸mysql
[root@db01 support-files]# mysql -uroot -p123456
# 若是有密碼,修改現有的密碼
[root@db01~]# mysqladmin -uroot -p123456 password 12345678
# 爲了安全起見,登陸採用以下方式
[root@db01 support-files]# mysql -uroot -p Enterpassword: Welcometo the MySQL monitor. Commands end with; or \g.YourMySQL connection id is 5 mysql>
十一、安全優化mysql步驟
1)修改root密碼:
# 設置密碼
[root@db01 support-files]# mysqladmin -uroot password 123456 [root@db01 support-files]#
# 若是有密碼,修改現有的密碼
[root@db01~]# mysqladmin -uroot -p123456 password 12345678
2)刪除test庫:
mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | # mysql數據庫必備的庫,自動維護 |mysql | # 建立用戶等數據會存放在這裏 |performance_schema | # mysql數據庫必須,自動維護 |test | # 能夠刪除 +--------------------+ 4rows in set (0.00 sec) mysql># 刪除test庫 mysql>drop database test; Query OK, 0 rows affected (0.02 sec) mysql>show databases; +--------------------+ |Database | +--------------------+ |information_schema | |mysql | |performance_schema | +--------------------+ 3rows in set (0.00 sec) mysql>
3)刪除無用的用戶,刪除用戶須要主機,大寫沒法刪除,特殊字符沒法刪除,此時須要使用特殊的SQL語句刪除
(delete from mysql.user where user=’root’ andhost=’A用戶’):
# 查詢當前數據庫裏的用戶
# 下面的顯示裏,root重複,在mysql裏user +host肯定一個惟一的用戶
# root localhost和root db01不是一個用戶
mysql>select user,host from mysql.user; +------+------------+ |user | host | +------+------------+ |root | 127.0.0.1 | |root | ::1 | | | localhost | |root | localhost | | | db01 ||root | db01 |+------+------------+6rows in set (0.00 sec) mysql>
# 刪除無用的用戶
mysql>drop user ""@localhost; QueryOK, 0 rows affected (0.00 sec) mysql>select user,host from mysql.user; +------+------------+ |user | host | +------+------------+ |root | 127.0.0.1 | |root | ::1 | # ::1表示IPV6的意思,這個用戶無用,能夠刪除 |root | localhost | | | web-lnmp02 | |root | web-lnmp02 | +------+------------+ 5rows in set (0.00 sec)
# 有時候,使用雙引號」」會報錯,因此使用單引號’’會好些
mysql>drop user ""@web-lnmp02; ERROR1064 (42000): You have an error in your SQL syntax; check the manual thatcorresponds to your MySQL server version for the right syntax to use near ‘-lnmp02‘at line 1 mysql> mysql> mysql>drop user ‘‘@‘web-lnmp02‘; QueryOK, 0 rows affected (0.00 sec) mysql> mysql>drop user ‘root‘@‘web-lnmp02‘; QueryOK, 0 rows affected (0.00 sec) mysql> mysql>drop user ‘root‘@‘::1‘; QueryOK, 0 rows affected (0.00 sec) mysql>
# 刪除用戶後,最終檢查,保留以下用戶
mysql>select user,host from mysql.user; +------+-----------+ |user | host | +------+-----------+ |root | 127.0.0.1 | |root | localhost | +------+-----------+ 2rows in set (0.00 sec) mysql>
# 若是錯把root localhost用戶刪除,能夠經過命令建立出來
mysql>grant all on *.* to ‘root’@localhost identified by ‘123456’ with grant option;
# 刷新mysql系統權限相關表
mysql>flush privileges;
十二、mysql簡單應用命令總結
1)Linux查看命令幫助使用man,內置命令使用help;在mysql中,使用help查看mysql的命令幫助:
mysql>help drop; Manyhelp items for your request exist. Tomake a more specific request, please type ‘help <item>‘, where<item> is one of the following topics: ALTER TABLE ALTER TABLESPACE DEALLOCATE PREPARE DROP DATABASE DROP EVENT DROP FUNCTION DROP FUNCTION UDF DROP INDEX DROP LOGFILE GROUP DROP PROCEDURE DROP SERVER DROP TABLE DROP TABLESPACE DROP TRIGGER DROP USER DROP VIEW mysql>help drop user; Name:‘DROP USER‘ Description: Syntax: DROPUSER user [, user] ... TheDROP USER statement removes one or more MySQL accounts and their privileges.It removes privilege rows for the account from all grant tables.To use this statement, you must have the global CREATE USER privilegeor the DELETE privilege for the mysql database. Each account nameuses the format described in http://dev.mysql.com/doc/refman/5.5/en/account-names.html.For example: # 幫助裏的例子 DROP USER ‘jeffrey‘@‘localhost‘; Ifyou specify only the user name part of the account name, a host name partof ‘%‘ is used. URL:http://dev.mysql.com/doc/refman/5.5/en/drop-user.html mysql>
2)檢查命令
mysql>show databases; # 顯示全部庫 mysql>use mysql; # 切換到mysql庫 mysql>show tables; # 顯示當前庫中全部表 mysql>select user,host from mysql.user; # 查看用戶列表 mysql>select user(); # 查看當前用戶 mysql>select database(); # 查看當前所在庫 mysql>drop database 庫名; # 刪除數據庫 mysql>drop user ’用戶’@’主機’ # 刪除用戶 # 刪除用戶 mysql>deletefrom mysql.user where user=」root」 and host=「A用戶」
本文出自 「Linuxgao」 博客,請務必保留此出處http://linuxgao.blog.51cto.com/11934904/1931007
原文:http://linuxgao.blog.51cto.com/11934904/1931007