centOS 部署服务器(三)
今天一个新的项目终于能够重新安装mysql了,分享下步骤:
1.下载地址:http://dev.mysql.com/downloads/mysql/ (选择Linux - Generic版本的Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed)
2.直接将下载后的解压缩到/usr/local目录下
tar zxvf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz -C /usr/local
mv mysql-5.7.15-linux-glibc2.5-x86_64 mysql
mkdir /var/lib/mysql
3.生成并修改mysql配置文件,cp support-files/my-default.cnf /etc/my.cnf
vim /etc/my.cnf:
[mysqld]
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
character-set-server=utf8
collation-server=utf8_general_ci
[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/run/mysql/mysql.pid
# include all files from the config directory
!includedir /etc/my.cnf.d
4.由于解压后的mysql事已经编译过的了,所以不需要进行--configure ,make ,make install等步骤了,直接进入到/usr/local/mysql目录下面进行下列指令操作(可参考mysql官网:http://dev.mysql.com/doc/refman/5.7/en/binary-installation.html):
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
mkdir data
mkdir mysql-files
chown -R mysql .
chgrp -R mysql .
bin/mysqld --initialize --user=mysql(记录好在这里生成的password,这便是mysql root用户的初始密码)
bin/mysql_ssl_rsa_setup
chown -R root .
chown -R mysql data mysql-files
bin/mysqld_safe --user=mysql &
注:在执行bin/mysqld --initalize --user=mysql的时候可能会出现如下bug:
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
这是因为少了libaio这个依赖包,只需yum -y install libaio即可
5.建立软连接 ln -s /usr/local/mysql/bin/mysql /usr/local/bin
mysql -uroot -p (输入之前记录的密码)
成功登录到mysql界面,但是发现操作任何命令始终提示You must reset your password using ALTER USER statement before executing this statement
解决方法如下:
step 1: set password = password('your new password');
step 2: alter user 'root'@'localhost' password expire never ;
step 3: flush privileges;
退出后重新登录mysql即可,密码为你新设置的密码
6.授权用户(如果没有该用户则会默认创建用户和密码)
create database basename
grant all privileges on basename.* to "usertest"@'%' identified by '123456'
这样就创建了用户名为usertest,密码为123456的用户,并且它可以在任何ip地址上登录(设置的是%)
7.设置启动(/usr/lib/systemd/system,适用于centos7)
(1)vim nginx.service
[Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target
启动,关闭,重启命令:systemctl start nginx systemctl stop nginx systemctl restart nginx
(2)vim mysql.service
[Unit] Description=mysql After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/mysql/support-files/mysql.server start ExecReload=/usr/local/mysql/support-files/mysql.server restart ExecStop=/usr/local/mysql/support-files/mysql.server stop #PrivateTmp=true [Install] WantedBy=multi-user.target
启动,关闭,重启命令:systemctl start mysql systemctl stop mysql systemctl restart mysql
(3)vim php-fpm.service
[Unit] Description=php After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/php/sbin/php-fpm ExecReload=/usr/local/php/sbin/php-fpm restart ExecStop=/usr/local/php/sbin/php-fpm stop [Install] WantedBy=multi-user.target
启动,关闭,重启命令:systemctl start php-fpm, systemctl stop php-fpm, systemctl restart php-fpm
(4)vim redis.service
[Unit] Description=Redis After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/bin/redis-server /etc/redis/6379.conf
ExecStop=kill -INT `cat /tmp/redis.pid` User=www Group=www [Install] WantedBy=multi-user.target
启动,关闭,重启命令:systemctl start redis, systemctl stop php-fpm, systemctl restart redis
编辑了或新增了service文件后需要systemctl daemon-reload一下service才会起作用