Linux下绿色安装MySQL

一.准备:

CentOS7系统第
mysql安装包,这里使用mysql 5.7.17(下载地址:http://dev.mysql.com/downloads/mysql/),如图:
mysql下载

二.环境搭建:
1.创建组及用户:
此步可以跳过,但是为了方便管理mysql,也为了用在正式的生产环境中,且处于安全考虑,这里为mysql单独建立了一个组及用户:

groupadd mysqlgroup  //新建一个mysqlgroup组
useradd -g mysqlgroup mysqluser  //创建一个名叫mysqluser的用户,将其归为mysqlgroup组

2.安装MySQL 5.7.17:
直接将下载的MySQL 5.7.17解压即可。

tar -xvzf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

由于解压后的名字很长(mysql-5.7.17-linux-glibc2.5-x86_64),我们将它重命名为mysql,并将其移动到/usr/local 目录

mv mysql-5.7.17-linux-glibc2.5-x86_64 /usr/local/mysql

更改mysql目录下所有的目录及文件夹所属组合用户

chown -R mysqluser:mysqlgroup mysql //将mysql目录的所属权更改为mysqlgroup下的mysqluser用户
chmod -R 777 mysql  //赋予mysql目录读写权限

3.创建日志目录

mkdir /var/log/mariadb
cd /var/log/mariadb/
touch mariadb.log
chmod -R 775 mariadb.log
chown -R mysql:mysql mariadb.log

chown -R mysqluser:mysqlgroup /var/log/mariadb/
mkdir /var/run/mariadb
chown -R mysqluser:mysqlgroup /var/run/mariadb/

4.初始化MySQL

 cd /usr/local/mysql
./bin/mysqld --initialize --user=mysqluser --basedir=/usr/local/mysql

第一次初始化时,最后会显示这样的字样“A temporary password is generated for….”,显示root的临时密码。
5.启动数据库

./bin/mysqld_safe --user=mysqluser

6.启动服务

[root@dbserver mysql]# cd bin/
[root@dbserver bin]# ./mysqld_safe --user=mysql &
[2] 10436

7.将mysqld服务加入开机自启动项。

[root@dbserver support-files]# cp mysql.server /etc/init.d/mysql  
[root@dbserver support-files]# chmod +x /etc/init.d/mysql 
-- 把mysql注册为开机启动的服务
[root@dbserver support-files]# chkconfig --add mysql  
-- 查看是否添加成功
[root@dbserver support-files]#  chkconfig --list mysql  
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off

8.启动服务

[root@dbserver bin]# service mysql start

9.将mysqld服务加入开机自启动项
将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限,这样就可以使用service mysql命令启动/停止服务,
否则就只能使用{mysql}/bin/mysqld_safe &命令来启动服务
还需要把mysql.server中basedir的相关路径,改为自定义的路径,默认路径是/usr/local/mysql

[root@dbserver support-files]# cp mysql.server /etc/init.d/mysql  
[root@dbserver support-files]# chmod +x /etc/init.d/mysql 
-- 把mysql注册为开机启动的服务
[root@dbserver support-files]# chkconfig --add mysql  
-- 查看是否添加成功
[root@dbserver support-files]#  chkconfig --list mysql  
Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysql           0:off   1:off   2:on    3:on    4:on    5:on    6:off

10.登录mysql

[root@dbserver bin]# ./mysql -u root -p
密码

11.设置密码

set password for 'root'@'localhost' =password('123456');//更改root密码的sql语句,123456是新的root密码(别忘了结尾的;号)

12.设置远程登录权限

mysql>  grant all privileges on *.* to'root' @'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.06 sec)

mysql> quit
Bye

13.开启防火墙端口 3306

3306端口放行 
        /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
        //将该设置添加到防火墙的规则中
        /etc/rc.d/init.d/iptables save

猜你喜欢

转载自blog.csdn.net/dian853013397/article/details/79929609
今日推荐