Linux軟件的安裝方式:
a. yum/rpm 特點:簡單,快,無法定製。
b. 編譯安裝 ./configure; make; make install 特點:複雜,速度慢,可定製,針對mysql,是第一條產品線的編譯方式
c. cmake安裝 mysql 5.5以上的版本, ./cmake; gmake ;gmake install
d. 二進制包安裝,直接解壓就能使用(相當於綠色軟件,無需安裝)
今天主要安裝的方式選擇二進制包。
安裝環境:Centos 6.5
軟件包名:mysql-5.5.32-linux2.6-x86_64.tar.gz
下載鏈接:http://pan.baidu.com/s/1c12HUAs 密碼:3yfz
1、創建mysql使用用戶
[root@Qinglin-Test1 tools]# useradd -s /sbin/nologin -M mysql
2、解壓MySQL壓縮包
[root@Qinglin-Test1 tools]# tar -xvf mysql-5.5.32-linux2.6-x86_64.tar.gz [root@Qinglin-Test1 tools]# ls mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32-linux2.6-x86_64.tar.gz
3、mv移動走,因爲是進制包,所以不需要安裝,移動到application目錄中
另需要創建軟連接,使用ln -s,必須寫全路徑
操作到這此步,相當於make與make install
[root@Qinglin-Test1 tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32-linux2.6-x86_64 [root@Qinglin-Test1 application]# ln -s /application/mysql-5.5.32-linux2.6-x86_64/ /application/mysql [root@Qinglin-Test1 application]# ll total 8 lrwxrwxrwx 1 root root 42 Jun 30 21:40 mysql -> /application/mysql-5.5.32-linux2.6-x86_64/ drwxr-xr-x 13 root root 4096 Jun 30 21:36 mysql-5.5.32-linux2.6-x86_64
4、查看初始化幫助文檔,瞭解自己要加的編譯參數
[root@Qinglin-Test1 /]# /application/mysql/scripts/mysql_install_db --help Usage: /application/mysql/scripts/mysql_install_db [OPTIONS] --basedir=path The path to the MySQL installation directory. --builddir=path If using --srcdir with out-of-directory builds, you will need to set this to the location of the build directory where built files reside. --cross-bootstrap For internal use. Used when building the MySQL system tables on a different host than the target. --datadir=path The path to the MySQL data directory. --defaults-extra-file=name Read this file after the global files are read. --defaults-file=name Only read default options from the given file name. --force Causes mysql_install_db to run even if DNS does not work. In that case, grant table entries that normally use hostnames will use IP addresses. --help Display this help and exit. --ldata=path The path to the MySQL data directory. Same as --datadir. --no-defaults Don't read default options from any option file. --rpm For internal use. This option is used by RPM files during the MySQL installation process. --skip-name-resolve Use IP addresses rather than hostnames when creating grant table entries. This option can be useful if your DNS does not work. --srcdir=path The path to the MySQL source directory. This option uses the compiled binaries and support files within the source tree, useful for if you don't want to install MySQL yet and just want to create the system tables. --user=user_name The login username to use for running mysqld. Files and directories created by mysqld will be owned by this user. You must be root to use this option. By default mysqld runs using your current login name and files and directories that it creates will be owned by you.
5、初始化數據庫
/application/mysql/scripts/mysql_install_db:指定安裝的命令
–basedir=指定mysql的安裝目錄
–datadir=data目錄,存放mysql數據文件的位置
–user=指定使用的用戶
以下執行的重要標識就是兩個OK,如下出現兩個OK,初步可以判斷安裝成功。
注意:如果這個位置出現錯誤,第一查看根下的/tmp是否是1777權限,第二嘗試修改Mysql目錄權限,第三修改hosts
一般都是因爲/tmp寫不進去數據造成的。
如下報的警告是hosts主機名的問題,可以忽略。
[root@Qinglin-Test1 /]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql WARNING: The host 'Qinglin-Test1' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK To start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER ! To do so, start the server, then issue the following commands: /application/mysql//bin/mysqladmin -u root password 'new-password' ==>設置密碼命令 /application/mysql//bin/mysqladmin -u root -h Qinglin-Test1 password 'new-password'==>改密碼命令 Alternatively you can run: /application/mysql//bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. This is strongly recommended for production servers. See the manual for more instructions. You can start the MySQL daemon with: cd /application/mysql/ ; /application/mysql//bin/mysqld_safe & ==>啓動mysql命令 You can test the MySQL daemon with mysql-test-run.pl cd /application/mysql//mysql-test ; perl mysql-test-run.pl ==>測試mysql命令 Please report any problems with the /application/mysql//scripts/mysqlbug script!
6、授權MySQL管理數據庫文件
[root@Qinglin-Test1 /]# chown -R mysql.mysql /application/mysql
7、生成MySQL配置文件
[root@Qinglin-Test1 /]# cp /application/mysql/support-files/my-small.cnf /etc/my.cnf cp: overwrite `/etc/my.cnf'? y
8、配置啓動MySQL
因爲mysqld_safe這個腳本默認使用的是/usr/local/mysql,所以啓動腳本時會找這裏,但是我們變換了路徑,所以需要修改替換 /usr/local/ 爲/application/mysql,這樣腳本才生效。
[root@Qinglin-Test1 /]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe
9、啓動mysql服務
[root@Qinglin-Test1 /]# /application/mysql/bin/mysqld_safe & [1] 18352 [root@Qinglin-Test1 /]# 160630 22:18:40 mysqld_safe Logging to '/application/mysql/data/Qinglin-Test1.err'. 160630 22:18:41 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
10、檢查服務與端口
[root@Qinglin-Test1 /]# ps -ef|grep mysql root 18352 18175 0 22:18 pts/0 00:00:00 /bin/sh /application/mysql/bin/mysqld_safe mysql 18570 18352 0 22:18 pts/0 00:00:00 /application/mysql/bin/mysqld --basedir=/application/mysql --datadir=/application/mysql/data --plugin-dir=/application/mysql/lib/plugin --user=mysql --log-error=/application/mysql/data/Qinglin-Test1.err --pid-file=/application/mysql/data/Qinglin-Test1.pid --socket=/tmp/mysql.sock --port=3306 root 18588 18175 0 22:19 pts/0 00:00:00 grep --color=auto mysql
11、配置環境變量
[root@Qinglin-Test1 /]# vim /etc/profile 切換到最後一行,添加 PATH="/application/mysql/bin:$PATH" [root@Qinglin-Test1 /]# source /etc/profile
12、測試登錄
[root@Qinglin-Test1 /]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.32 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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安裝擴展知識
13、配置傳統方式啓動MySQL
思路:拷貝,然後添加執行權限,然後killall掉之前啓動的mysql服務,最好再重複上面的檢查一下,使用ps命令
[root@Qinglin-Test1 /]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld [root@Qinglin-Test1 /]# chmod +x /etc/init.d/mysqld [root@Qinglin-Test1 /]# killall mysqld
14、傳統啓動MySQL並加開機自啓動選項。
注意:這裏將拷貝到init.d的mysql腳本中的路徑替換,和之前的問題一樣,如果不替換的話找不到文件,並會報錯。
[root@Qinglin-Test1 /]# sed -i 's#/usr/local/mysql#/application/mysql#g' /etc/init.d/mysqld [root@Qinglin-Test1 /]# /etc/init.d/mysqld start Starting MySQL.. [ OK ] [root@Qinglin-Test1 /]# chkconfig mysqld on [root@Qinglin-Test1 /]# chkconfig --list |grep mysql mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
15、設置MySQL的root密碼和更改密碼,並登陸
root@Qinglin-Test1 /]# mysqladmin -uroot password "123456" [root@Qinglin-Test1 /]# mysqladmin -uroot -p'123456' password 'qinglin' [root@Qinglin-Test1 /]# mysql -uroot -p'qinglin' Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 7 Server version: 5.5.32 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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>
16、安全優化
刪除默認的test數據庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec) mysql> drop database test; Query OK, 0 rows affected (0.01 sec)
刪除無用用戶(保留 root),不使用drop的原因是裏成包含大寫drop不識別,且不夠快捷。
mysql> select user,host from mysql.user; +------+---------------+ | user | host | +------+---------------+ | root | 127.0.0.1 | | root | ::1 | | | Qinglin-Test1 | | root | Qinglin-Test1 | | | localhost | | root | localhost | +------+---------------+ 6 rows in set (0.00 sec) mysql> delete from mysql.user where user=''; Query OK, 0 rows affected (0.00 sec) mysql> delete from mysql.user where host='Qinglin-Test1'; Query OK, 0 rows affected (0.00 sec) mysql> delete from mysql.user where host='::1'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec)
FAQ
問題1:
問題原因:初始化問題
解決方法:刪除data目錄重建,重新初始化
[root@Qinglin-Site ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
問題2:
問題原因:權限問題
解決方法:chmod -R 1777 /tmp
ERROR: 1 Can't create/write to file
問題3:
問題原因:上傳錯誤
解決方法:rz 不要打勾。ASCII
問題4:
問題原因:ERROR 20002錯誤
解決方法:服務沒有啓動
ERROR 2002(HY000): Can't connect to local MySQL server through socket ...