创建安装目录
cd /usr/local/
mkdir mysql
cd mysql
下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
解压安装包
tar vxzf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
配置mysql
建立mysql运行对应的用户和用户组,并创建数据库
建立用户mysql,组mysql 。后面mysql就使用这个用户来运行
(注意这也是mysql启动脚本中默认的用户,因此最好不要改名,使用-r参数表示mysql用户是一个系统用户,不能登录)
groupadd mysql
useradd -r -g mysql mysql
创建数据库目录
mkdir -p /var/mysql/data
chown -R mysql:mysql /var/mysql
cd /usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
初始化数据库
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64 --datadir=/var/mysql/data
如果出现以下错误:
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file:
解决方法:
[root@xxx]# yum install -y libaio // 安装后在初始化就可以了
把mysql加入搜索路径
在/etc/profile中增加以下行,把mysql加入搜索路径
# mysql
export MYSQL_HOME=/usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
export PATH=${MYSQL_HOME}/bin:$PATH
source /etc/profile 使配置文件生效
配置mysql
拷贝mysql配置文件
cd /usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
cp support-files/my-default.cnf /etc/my.cnf
如果不存在my.cnf,则不用拷贝,直接修改 /etc/my.cnf文件
修改my.cnf,主要配置如下
[mysqld]
datadir=/var/mysql/data
socket=/var/mysql/data/mysql.sock
Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
interactive_timeout=3000
wait_timeout=3000
character-set-server=utf8
collation-server=utf8_general_ci
#Settings user and group are ignored when systemd is used.
#If you need to run mysqld under a different user or group,
#customize your systemd unit file for mariadb according to the
#instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
# log-error=/var/log/mariadb/mariadb.log
# pid-file=/var/run/mariadb/mariadb.pid
log-error=/var/log/mysql/mysql.log
pid-file=/var/mysql/data/mysql.pid
[client]
socket=/var/mysql/data/mysql.sock
# include all files from the config directory
!includedir /etc/my.cnf.d
为mysql的log准备空间
cd /var/log
mkdir mysql
chown -R mysql:mysql ./mysql/
配置系统启动后自动启动mysqld
拷贝mysqld的配置文件
cd /usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
修改/etc/init.d/mysqld
vim /etc/init.d/mysqld,把其中两行修改为:
basedir=/usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
datadir=/var/mysql/data
设定开机启动,并启动
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
检查mysqld服务已经启动
ps -ef|grep mysqld
netstat -lnp | grep -i mysql
[root@xxx mysql-5.7.18-linux-glibc2.5-x86_64]# netstat -anp | grep 3306
tcp6 0 0 :::3306 :::* LISTEN 9897/mysqld
[root@xxx mysql-5.7.18-linux-glibc2.5-x86_64]# service mysqld status
MySQL running (9897) [ OK ]
[root@xxx mysql-5.7.18-linux-glibc2.5-x86_64]#
客户端连接测试
cd /usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
bin/mysql -uroot -p
如果出现错误:ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘***’ (2)
解决办法 :
确定“socket”文件正确位置:
确定mysql服务正常运行后,产生此错误的原因只剩下“socket”文件路径不正确了,我们可以使用“find”命令或者“lsof”命令来确定socket文件的正确路径:
[root@xxx mysql-5.7.18-linux-glibc2.5-x86_64]# find / -name '*.sock'
/var/mysql/data/mysql.sock
修改“/etc/my.cnf”配置文件,在配置文件中添加“[client]”选项和“[mysql]”选项,并使用这两个选项下的“socket”参数值,与“[mysqld]”选项下的“socket”参数值,指向的socket文件路径完全一致。如下:
...
[client]
default-character-set=utf8
socket=/var/mysql/data/mysql.sock
[mysql]
default-character-set=utf8
socket=/var/mysql/data/mysql.sock
可参考:http://aiezu.com/article/mysql_cant_connect_through_socket.html
修改mysql的登录密码
ALTER USER 'root'@'localhost' IDENTIFIED BY 'abcdefg12345l';