1、下载,解压
把下载的文件放到 /app/programs/目录下
tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
地址:https://dev.mysql.com/downloads/mysql/
2、建目录
/usr/local/mysql
3、将解压文件复制到新建目录
cp -r mysql-5.7.22-linux-glibc2.12-x86_64/ /usr/local/mysql/
4、创建用户组和用户
groupadd mysql
useradd -r -g mysql mysql
5、创建data目录
cd /usr/loca/mysql
mkdir data
6、修改目录权限
cd /usr/local/mysql
chown -R mysql:mysql ./
7、初始化
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
注意生成的密码:HyjrkTcs8d>4
8、配置my.cnf
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/tmp/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # 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=/usr/local/mysql/mysqld.log pid-file=/usr/local/mysql/data/mysqld.pid # # include all files from the config directory # !includedir /etc/my.cnf.d
9、启动
cd /usr/local/mysql/bin
./mysqld_safe --user=mysql &
10、登录修改密码 mysql -uroot -p 上面初始化时的密码
alter user 'root'@'localhost' identified by 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root1'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; #授权新用户
flush privileges; #刷新权限
11、设置开机启动
cd /usr/local/mysql/support-files/
cp mysql.server /etc/init.d/mysql
vi /etc/init.d/mysql
将mysql目录填上:
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
授权:chmod +x /etc/init.d/mysql
设为开机启动:chkconfig --add mysql
12、service启动和关闭
重启服务:service mysql restart
停止服务:service mysql stop
启动服务:service mysql start
查看服务:service mysql status
13、mysql命令 在任意目录执行
ln -s /usr/local/mysql/bin/mysql /usr/bin/
问题:
1、遇到mysql进程杀不死的情况
杀不死的情况是因为用守护线程起的服务,即通过mysqld_safe启动的。可以通过mysql -uroot -p先登录进去,然后执行 shutdown命令。退出mysql窗口,重新启动服务:service mysql start
2、mysql.sock的问题
安装后,用root登录时提示‘/tmp/mysql.sock’,
解决1: /etc/my.cnf中 配置成:socket=/var/lib/mysql/mysql.sock,同时建立软链:ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
解决2:在my.cnf中,增加配置,如下:
[mysql]
socket=/var/lib/mysql/mysql.sock
3、阿里云服务器mysql
如果是在阿里云服务器安装的mysql,外部访问的时候,会提示连不上,需要在阿里云服务器配置一下安全组