建立安装目录html
cd /usr/local/ mkdir mysql cd mysql
下载安装包java
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
解压安装包mysql
tar vxzf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
配置mysqllinux
创建mysql运行对应的用户和用户组,并建立数据库sql
创建用户mysql,组mysql 。后面mysql就使用这个用户来运行数据库
(注意这也是mysql启动脚本中默认的用户,所以最好不要更名,使用-r参数表示mysql用户是一个系统用户,不能登陆)vim
groupadd mysql useradd -r -g mysql mysql
建立数据库目录socket
mkdir -p /var/mysql/data chown -R mysql:mysql /var/mysql cd /usr/local/mysql/mysql-5.7.18-linux-glibc2.5-x86_64
初始化数据库tcp
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';