Linux安装mysql

1): 查看系统自带的Mariadb

[root@test01 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64

2): 卸载系统自带的Mariadb

[root@test01 ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

3): 删除etc目录下的my.cnf

[root@test01 ~]# rm /etc/my.cnf

4): 检查mysql是否存在

[root@test01 ~]# rpm -qa | grep mysql

5):检查mysql组合用户是否存在,如无则创建

[root@test01 ~]# cat /etc/group | grep mysql
[root@test01 ~]# cat /etc/passwd | grep mysql 

6):创建mysql用户组

[root@test01 ~]# groupadd mysql

7):创建一个用户名为mysql的用户,并加入mysql用户组

[root@test01 ~]# useradd -g mysql mysql
# 制定password 为root
[root@test01 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

8):下载mysql离线安装包tar文件

# 官网下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
Select Version:5.7.27
	Select Operating System:Red Hat Enterprise Linux / Oracle Linux
	Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
	列表中下载:
	Compressed TAR Archive:(mysql-5.7.27-el7-x86_64.tar.gz)

9):上传下载的mysql TAR包

# 进入/usr/local/文件夹
[root@test01 ~]# cd /usr/local/
# 上传mysql TAR包
[root@test01 local]# rz
# 解压mysql-5.7.27-el7-x86_64.tar.gz
[root@test01 local]# tar -zxvf mysql-5.7.27-el7-x86_64.tar.gz
# 进入/usr/local下,修改为mysql

[root@test01 local]# mv mysql-5.7.27-el7-x86_64 mysql

10): 更改所属的组和用户

[root@test01 ~]# cd /usr/local/
[root@test01 local]# chown -R mysql mysql/
[root@test01 local]# chgrp -R mysql mysql/
[root@test01 local]# cd mysql/
[root@test01 mysql]# mkdir data
[root@test01 mysql]# chown -R mysql:mysql data

10):在/etc下创建my.cnf文件

[root@test01 ~]# cd /usr/local/mysql


# 编辑my.cnf
[root@test01 mysql]# vim my.conf
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8

[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port  
port = 3306 # set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true

[mysql.server]
user=mysql
basedir=/usr/local/mysql
[root@test01 mysql]# 

11):安装mysql

# 进入mysql
[root@test01 local]# cd /usr/local/mysql
# 安装mysql
[root@test01 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty:
2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
#设置文件及目录权限:
[root@test01 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@test01 mysql]# chown 777 my.cnf
[root@test01 mysql]# chmod +x /etc/init.d/mysqld
[root@test01 mysql]#
[root@test01 mysql]# mkdir data
[root@test01 mysql]#
[root@test01 mysql]# chown -R mysql:mysql data
[root@test01 mysql]# 

12):启动mysql

[root@test01 mysql]# /etc/init.d/mysqld start
Starting MySQL..[  OK  ]

13):设置开机启动

[root@test01 mysql]# chkconfig --level 35 mysqld on
[root@test01 mysql]# chkconfig --list mysqld
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]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@test01 mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@test01 mysql]# chkconfig --add mysqld
[root@test01 mysql]# chkconfig --list mysqld

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]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@test01 mysql]# service mysqld status
MySQL running (26122)[  OK  ]
[root@test01 mysql]# 

14):修改配置文件

[root@test01 mysql]# vim /etc/profile

15):在最后添加如下内容

# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@test01 mysql]# source /etc/profile

16):获得mysql初始密码

[root@test01 mysql]#  cat /root/.mysql_secret  
# Password set for user 'root@localhost' at 2019-03-08 17:40:42
poc3u0mO_luv
[root@test01 mysql]# 
#修改密码
[root@test01 mysql]# mysql -uroot -p
Enter password: #此处填写上边获取到的初始密码‘poc3u0mO_luv’
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>  set PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> exit
Bye

#验证新密码是否登录成功:
[root@test01 mysql]# mysql -uroot -p
Enter password: #此处输入新密码‘123456’
mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

17): 添加远程访问权限

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 

18): 重启mysql

[root@test01 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[  OK  ]
Starting MySQL..[  OK  ]
[root@test01 mysql]# 

你可能感兴趣的:(linux)