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使用用户
[[email protected] tools]# useradd -s /sbin/nologin -M mysql
2、解压MySQL压缩包
[[email protected] tools]# tar -xvf mysql-5.5.32-linux2.6-x86_64.tar.gz [[email protected] 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
[[email protected] tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32-linux2.6-x86_64 [[email protected] application]# ln -s /application/mysql-5.5.32-linux2.6-x86_64/ /application/mysql [[email protected] 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、查看初始化帮助文档,了解自己要加的编译参数
[[email protected] /]# /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主机名的问题,可以忽略。
[[email protected] /]# /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管理数据库文件
[[email protected] /]# chown -R mysql.mysql /application/mysql
7、生成MySQL配置文件
[[email protected] /]# 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,这样脚本才生效。
[[email protected] /]# sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /application/mysql/bin/mysqld_safe
9、启动mysql服务
[[email protected] /]# /application/mysql/bin/mysqld_safe & [1] 18352 [[email protected] /]# 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、检查服务与端口
[[email protected] /]# 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、配置环境变量
[[email protected] /]# vim /etc/profile 切换到最后一行,添加 PATH="/application/mysql/bin:$PATH" [[email protected] /]# source /etc/profile
12、测试登录
[[email protected] /]# 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命令
[[email protected] /]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld [[email protected] /]# chmod +x /etc/init.d/mysqld [[email protected] /]# killall mysqld
14、传统启动MySQL并加开机自启动选项。
注意:这里将拷贝到init.d的mysql脚本中的路径替换,和之前的问题一样,如果不替换的话找不到文件,并会报错。
[[email protected] /]# sed -i ‘s#/usr/local/mysql#/application/mysql#g‘ /etc/init.d/mysqld [[email protected] /]# /etc/init.d/mysqld start Starting MySQL.. [ OK ] [[email protected] /]# chkconfig mysqld on [[email protected] /]# chkconfig --list |grep mysql mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
15、设置MySQL的root密码和更改密码,并登陆
[email protected] /]# mysqladmin -uroot password "123456" [[email protected] /]# mysqladmin -uroot -p‘123456‘ password ‘qinglin‘ [[email protected] /]# 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目录重建,重新初始化
[[email protected] ~]# 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 ...