检查是否已经安装mysql
rpm -qa| grep mysql
[root@test /]# rpm -qa | grep mysql
mysql-libs-5.1.71-1.el6.x86_64
卸载mysql
rpm -e mysql-libs –nodeps
在电脑中查找有关mysql的文件尽可能删掉,如果有的话
比如/etc/my.cc或者/etc/mysql
下载及解压安装包
官方下载地址为:http://dev.mysql.com/downloads/mysql/
Linux - Generic
(mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz)
我的安装目录是: /home/mysql
#安装libaio库
yum search libaio
yum install libaio
#移动mysql更目录到需要安装的地方
cd /home/setup/clear
tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
mv /home/setup/mysql-5.7.17-linux-glibc2.5-x86_64 /home/mysql
#复制配置文件
cp /home/mysql/support-files/my-default.cnf /etc/my.cnf
#修改配置文件 修改内容如下
vi /etc/my.cnf
basedir = /home/mysql/
datadir = /home/mysql/data
port = 3306
socket = /home/mysql/tmp/mysql.sock
#创建data,tmp目录
cd /home/mysql
mkdir data
mkdir tmp
#创建不可登陆用户及组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
#增加目录权限
cd /home/mysql
chown -R mysql .
chgrp -R mysql .
#复制mysqld
cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
#初始化数据库
cd /home/mysql/bin
./mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize
[Note] A temporary password is generated for root@localhost: b,prkho.N8Sm
#需要注意关于timestamp的设置--explicit_defaults_for_timestamp
#启动
service mysqld start
#如果报如下错误:则需要转到NEXT1
[root@test bin]# service mysqld start
Starting MySQL.Logging to ‘/home/mysql/data/test.myhome.m.hzwh.com.err‘.
2017-05-02T00:20:15.262870Z mysqld_safe The file /usr/local/mysql/bin/mysqld
does not exist or is not executable. Please cd to the mysql installation
directory and restart this script from there as follows:
./bin/mysqld_safe&
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information
ERROR! The server quit without updating PID file (/home/mysql/data/test.myhome.m.hzwh.com.pid).
#NEXT1
#创建/usr/local/mysql/bin目录
cd /usr/local
mkdir mysql
cd mysql
mkdir bin
#建立链接
ln -s /home/mysql/bin/mysqld /usr/local/mysql/bin/mysqld
#启动mysqld
service mysqld start
#客户端连接
cd /home/mysql/bin
./mysql -u root -p
Enter password:
这个也就是刚才的那个密码:b,prkho.N8Sm
如果报如下错误则转到NEXT2
[root@test bin]# ./mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/tmp/mysql.sock‘ (2)
#NEXT2
ln -s /home/mysql/tmp/mysql.sock /tmp/mysql.sock
./mysql -u root -p
#登陆成功
#修改root密码为123456
mysql>set password=password(‘123456‘);
#创建用户
mysql>CREATE USER ‘fswh‘@‘localhost‘ IDENTIFIED BY ‘Fswh888$‘;
#创建数据库test_db
mysql> CREATE DATABASE IF NOT EXISTS `fswh_vipsys` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
#用户授权数据库
#只容许本地访问
mysql> GRANT ALL PRIVILEGES ON fswh_vipsys.* TO fswh@‘localhost‘;
mysql> flush privileges;
#开启远程访问,在Linux下为了安全,默认是不允许mysql本机以外的机器访问mysql数据库服务,因此需要重新授权用户。
mysql> GRANT ALL PRIVILEGES ON fswh_vipsys.* TO fswh@‘%‘ IDENTIFIED BY ‘Fswh888$‘;
mysql> flush privileges;
#mysql 修改用户名密码
mysql> use mysql;
mysql> UPDATE user SET password=password("test123") WHERE user=‘root‘;
mysql> flush privileges;
mysql> exit;
#设置开机启动
#检查自启动项列表中没有mysqld这个,如果没有就添加mysqld
chkconfig --list mysqld
chkconfig --add mysqld
#设置MySQL在345等级自动启动
chkconfig --level 345 mysqld on
#或用这个命令设置开机启动
chkconfig mysqld on
#mysql服务的启动/重启/停止
service mysqld start
service mysqld restart
service mysqld stop