RPM方式安装MySQL5.6 (CentOS 6.6)
a. 检查MySQL及相关RPM包,是否安装,如果有安装,则移除(rpm –e 名称)
1 |
[root@localhost # rpm -qa | grep -i mysql
|
2 |
mysql-libs-5.1.66-2.el6_3.x86_64 |
3 |
... |
4 |
[root@localhost #rpm -e --nodeps mysql-libs* //*号指代实际包名, 请实际情况输入完整包名
|
5 |
[root@localhost #rpm -e mysql-server*
|
6 |
#如果有早期版本mysql, 则卸载相关组件 |
7 |
[root@localhost #rpm -e mysql-client*
|
检查并删除老版本mysql的开发头文件和库
命令:rm -fr /usr/lib/mysql
rm -fr /usr/include/mysql
注意:卸载后/var/lib/mysql中的数据及/etc/my.cnf不会删除,如果确定没用后就手工删除
rm -f /etc/my.cnf
rm -fr /var/lib/mysql
rm -fr /usr/share/mysql
b. 下载Linux对应的RPM包然后解压缩,如:CentOS6.6_64对应的RPM包(MySQL-5.6.26-1.el6.x86_64.rpm-bundle.tar),如下:
1 |
[root@localhost # cd /usr
|
2 |
[root@localhost # tar -xvf MySQL-5.6.26-1.el6.x86_64.rpm-bundle.tar
|
c. 安装MySQL
1 |
[root@localhost # rpm -ivh MySQL-server-5.6.15-1.el6.x86_64.rpm
|
2 |
[root@localhost # rpm -ivh MySQL-devel-5.6.15-1.el6.x86_64.rpm
|
3 |
[root@localhost # rpm -ivh MySQL-client-5.6.15-1.el6.x86_64.rpm
|
4 |
#修改配置文件位置 |
5 |
[root@localhost # cp /usr/share/mysql/my-default.cnf /etc/my.cnf
|
如果需要更改默认的data文件夹, 请提前更改my.cnf中datadir目录,
socket和pid-file建议还是使用默认的目录/var/lib/mysql
(注意: 如果没有经验请完全按照默认的配置, 无需对my.cnf做任何更改, 或者按照本文最后的my.cnf配置)
d. 初始化MySQL及设置密码
1 |
[root@localhost # /usr/bin/mysql_install_db //强烈建议用mysql用户运行脚本(su mysql) # /usr/bin/mysqld --initialize //mysql 5.7 如果有修改默认data存放地址, 一定要做修改以下设置, or as default 1.1 创建data文件存地址, 并修改用户和组为mysql chown mysql:mysql /home/mysqldata -R 1.2. 关闭selinux或者设置新数据文件权限, 同时尝试设置权限755 1.3 修改默认地址/etc/rc.d/init.d/mysql (如果启动失败, 可以尝试延后当前步骤) datadir=/home/mysqldata chcon -Rt mysqld_db_t /home/mysqldata |
2 |
[root@localhost # service mysql start |
3 |
[root@localhost # cat /root/.mysql_secret #查看root账号密码 |
4 |
[root@localhost # mysql -u root -p qKTaFZnl |
5 |
mysql> 'Aa12345' ); #设置密码为Aa12345 |
6 |
mysql> exit |
7 |
[root@localhost ~] # mysql -u root -p
|
e. 允许远程登陆
01 |
mysql> |
02 |
mysql> select host,user,password from user;
|
03 |
+-----------------------+------+-------------------------------------------+ |
04 |
| |
05 |
+-----------------------+------+-------------------------------------------+ |
06 |
| |
07 |
| |
08 |
| |
09 |
| |
10 |
+-----------------------+------+-------------------------------------------+ |
11 |
12 |
mysql> set password=password( 'Aa12345' ) where user= 'root' ;
|
13 |
mysql> set host= '%' where user= 'root' and host= 'localhost' ;
|
14 |
mysql> |
15 |
mysql> exit
|
f. 设置开机自启动
1 |
[root@localhost # chkconfig mysql on
|
2 |
[root@localhost # chkconfig --list | grep mysql
|
3 |
mysql |
g. MySQL的默认安装位置
1 |
/var/lib/mysql/ #数据库目录
|
2 |
/usr/share/mysql #配置文件目录
|
3 |
/usr/bin #相关命令目录
|
4 |
/etc/init.d/mysql #启动脚本
|
FAQ:
1. 端口号3306被占用, 更改端口号, 或者查看当前端口被哪个进程占用, 然后kill
#netstat -anp | grep "3306"
tcp 0 0 :::3306 :::* LISTEN 4280/mysqld
#kill 4280
2. 执行mysql -u root -p输入密码后显示错误
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
2.1 请使用临时命令 mysql -u root -p -S /home/mysqldata/mysql.sock\
2.2 指定my.cnf >[client] ->socket 路径
3. 修改默认地址/etc/rc.d/init.d/mysql
datadir=/home/mysqldata
4. 提示权限问题, 关闭selinux或者设置新数据文件权限
chcon -Rt mysqld_db_t /home/mysqldata
5. 最近新装好的mysql在进入mysql工具时,总是有错误提示:
# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
或者
# mysql -u root -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' and host='root' or host='localhost';//把空的用户密码都修改成非空的密码就行了。
mysql> FLUSH PRIVILEGES;
mysql> quit # /etc/init.d/mysqld restart
# mysql -u root -p
Enter password: <输入新设的密码newpassword>
修改字符集和数据存储路径
配置/etc/my.cnf文件,修改数据存放路径、mysql.sock路径以及默认编码utf-8.
#查看字符集
show variables like '%collation%';
show variables like '%char%';
SHOW VARIABLES LIKE '%character%'
#查看默引擎
show engines
#查看默认包大小
show VARIABLES like '%max_allowed_packet%';
#查看log文件地址
ls -l `pgrep mysqld | sed 's@.*@/proc/&/fd/@'` | grep log
#查看插件地址
show variables like 'plugin_dir'
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
character-set-server=utf8
default-storage-engine=INNODB
max_allowed_packet = 32M
slave_max_allowed_packet = 1024M
datadir=/home/mysqldata
socket=/home/mysqldata/mysql.sock
max_connections = 1000
#Master configuration-defaults
#server-id=1 #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
#log_bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=10000
#Slave configuration-defaults
#server-id=2 #设置服务器id,为2表示从服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。
#rpl_semi_sync_slave_enabled=1
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 45G
innodb_log_file_size=1024M
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/home/mysqldata/mysqld.pid
[client]
socket=/home/mysqldata/mysql.sock