Green installation of MySQL under Linux

1. Preparation:

The mysql installation package of CentOS7 system
, use mysql 5.7.17 here (download address: http://dev.mysql.com/downloads/mysql/ ), as shown in the figure:
mysql download

2. Environment construction:
1. Create groups and users:
This step can be skipped, but in order to facilitate the management of mysql, and for use in a formal production environment, and for security reasons, here is a separate group and user for mysql:

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

2. Install MySQL 5.7.17:
Decompress the downloaded MySQL 5.7.17 directly.

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

Since the unpacked name is long (mysql-5.7.17-linux-glibc2.5-x86_64), we renamed it to mysql and moved it to the /usr/local directory

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

Change the combined user of all directories and folders in the mysql directory

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

3. Create a log directory

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. Initialize MySQL

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

When initialized for the first time, the words "A temporary password is generated for...." will be displayed at the end, showing the temporary password of root.
5. Start the database

./bin/mysqld_safe --user=mysqluser

6. Start the service

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

7. Add the mysqld service to the boot auto-start item.

[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. Start the service

[root@dbserver bin]# service mysql start

9. Add the mysqld service to the boot auto-start item.
Copy {mysql}/support-files/mysql.server to /etc/init.d/mysql and set the running permission, so that you can use the service mysql command to start/stop the service,
otherwise You can only use the {mysql}/bin/mysqld_safe & command to start the service. You
also need to change the relative path of basedir in mysql.server to a custom path. The default path is /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. Login to mysql

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

11. Set a password

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

12. Set remote login permissions

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. Open firewall port 3306

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324781680&siteId=291194637