linux安装mysql

Linux中安装mysql

1、 删除mariadb(centos 7 自带的一个数据库)

[root@nat1 ~]# yum -y remove maria*

2、 查询是否删除,这行命令执行后无信息则证明已经删除了

[root@nat1 ~]# rpm -qa | grep maria*

3、查询系统中是否安装了mysql,这行命令执行后,无信息则证明没有安装

[root@nat1 etc]# rpm -qa | grep mysql
[root@nat1 etc]# 

4、确保etc目录下没有my.cnf;创建用户mysql,组mysql,为mysql用户设置密码

[root@nat1 /]# cd etc/
[root@nat1 etc]# find my.cnf
find: ‘my.cnf’: No such file or directory
[root@nat1 etc]# rpm -qa | grep mysql
[root@nat1 etc]# cat group | grep mysql
[root@nat1 etc]# cat passwd |grep mysql
[root@nat1 etc]# groupadd mysql
[root@nat1 etc]# useradd -g mysql mysql
[root@nat1 etc]# passwd mysql
Changing password for user mysql.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@nat1 etc]# 

5、将mysql的压缩包上传到/usr/local/mysql目录下,并解压

[root@nat1 mysql]# tar -zxvf mysql-5.7.26-el7-x86_64.tar.gz 

6、为mysql解压后的文件夹重命名

[root@nat1 mysql]# mv mysql-5.7.26-el7-x86_64 mysql5.7
[root@nat1 mysql]# ll
total 4348
drwxr-xr-x. 9 root root     129 Nov 14 05:07 mysql5.7
-rw-r--r--. 1 root root 4452049 Nov 14 04:52 mysql-connector-java-5.1.47.tar.gz

7、授权给mysql用户及组

[root@nat1 local]# ll
total 0
drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
drwxr-xr-x.  2 root root   6 Apr 11  2018 games
drwxr-xr-x. 15 root root 231 Nov  4 21:54 hadoop
-rw-r--r--.  1 root root   0 Nov  4 07:25 Hello
drwxr-xr-x.  2 root root   6 Apr 11  2018 include
drwxr-xr-x.  8   10  143 255 Dec 19  2017 java
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
drwxr-xr-x.  3 root root  64 Nov 14 05:09 mysql
drwxr-xr-x.  2 root root   6 Apr 11  2018 sbin
drwxr-xr-x.  5 root root  49 Oct 31 06:45 share
drwxr-xr-x.  2 root root  67 Nov  1 06:52 src
[root@nat1 local]# chown -R mysql:mysql mysql/
[root@nat1 local]# ll
total 0
drwxr-xr-x.  2 root  root    6 Apr 11  2018 bin
drwxr-xr-x.  2 root  root    6 Apr 11  2018 etc
drwxr-xr-x.  2 root  root    6 Apr 11  2018 games
drwxr-xr-x. 15 root  root  231 Nov  4 21:54 hadoop
-rw-r--r--.  1 root  root    0 Nov  4 07:25 Hello
drwxr-xr-x.  2 root  root    6 Apr 11  2018 include
drwxr-xr-x.  8    10   143 255 Dec 19  2017 java
drwxr-xr-x.  2 root  root    6 Apr 11  2018 lib
drwxr-xr-x.  2 root  root    6 Apr 11  2018 lib64
drwxr-xr-x.  2 root  root    6 Apr 11  2018 libexec
drwxr-xr-x.  3 mysql mysql  64 Nov 14 05:09 mysql
drwxr-xr-x.  2 root  root    6 Apr 11  2018 sbin
drwxr-xr-x.  5 root  root   49 Oct 31 06:45 share
drwxr-xr-x.  2 root  root   67 Nov  1 06:52 src

8、在解压目录下,创建data文件夹并授权

[root@nat1 mysql5.7]# mkdir data
[root@nat1 mysql5.7]# ll
total 36
drwxr-xr-x.  2 mysql mysql  4096 Nov 14 05:07 bin
-rw-r--r--.  1 mysql mysql 17987 Apr 13  2019 COPYING
drwxr-xr-x.  2 root  root      6 Nov 14 05:12 data
drwxr-xr-x.  2 mysql mysql    55 Nov 14 05:07 docs
drwxr-xr-x.  3 mysql mysql  4096 Nov 14 05:06 include
drwxr-xr-x.  5 mysql mysql   230 Nov 14 05:07 lib
drwxr-xr-x.  4 mysql mysql    30 Nov 14 05:06 man
-rw-r--r--.  1 mysql mysql  2478 Apr 13  2019 README
drwxr-xr-x. 28 mysql mysql  4096 Nov 14 05:07 share
drwxr-xr-x.  2 mysql mysql    90 Nov 14 05:07 support-files
[root@nat1 mysql5.7]# chown -R mysql:mysql data

9、在/etc下创建my.cnf文件并配置信息

[root@nat1 mysql5.7]# pwd
/usr/local/mysql/mysql5.7
[root@nat1 mysql5.7]# cd /etc
[root@nat1 etc]# vim my.cnf
[root@nat1 etc]# cat my.cnf 
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8 
[mysqld]
skip-name-resolve
#设置3306端口
port = 3306 
# 设置mysql的安装目录
basedir=/usr/local/mysql/mysql5.7
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/mysql5.7/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB 
lower_case_table_names=1
max_allowed_packet=16M

10、初始化

这里有日志,这种初始化方式(mysql_install_db)已经过时。使用下边这条命令初始化,则初始密码会在错误日志中。

参考:csdn的mysql安装

bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql5.7 --datadir=/usr/local/mysql/mysql5.7/data
[root@nat1 mysql5.7]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/mysql5.7 --datadir=/usr/local/mysql/mysql5.7/data
2019-11-14 05:20:44 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-11-14 05:20:47 [WARNING] The bootstrap log isn't empty:
2019-11-14 05:20:47 [WARNING] 2019-11-14T10:20:44.956430Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2019-11-14T10:20:44.957073Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2019-11-14T10:20:44.957081Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)

11、查看日志文件,授权、启动mysql

[root@nat1 mysql5.7]# cd data
[root@nat1 data]# ls
auto.cnf    cav3.ext         client-req.pem  ib_logfile1         server-key.pem
ca-key.pem  certv3.ext       ib_buffer_pool  mysql               server-req.pem
ca.pem      client-cert.pem  ibdata1         performance_schema  sys
ca-req.pem  client-key.pem   ib_logfile0     server-cert.pem
[root@nat1 data]# cd ..
[root@nat1 mysql5.7]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@nat1 mysql5.7]# chmod 755 /etc/my.cnf
[root@nat1 mysql5.7]# chmod +x /etc/init.d/mysqld 
[root@nat1 mysql5.7]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/mysql5.7/data/nat1.err'.
 SUCCESS! 

12、配置环境,MYSQL_HOME,/etc/profile

[root@nat1 mysql5.7]# vim /etc/profile
[root@nat1 mysql5.7]# source /etc/profile
[root@nat1 mysql5.7]# cat /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
    if [ -z "$EUID" ]; then
        # ksh workaround
        EUID=`/usr/bin/id -u`
        UID=`/usr/bin/id -ru`
    fi
    USER="`/usr/bin/id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then 
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
#java environment
export JAVA_HOME=/usr/local/java
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
#hadoop environemt
export HADOOP_HOME=/usr/local/hadoop
export PATH=$PATH:$HADOOP_HOME/bin
#mysql environment
export MYSQL_HOME=/usr/local/mysql/mysql5.7
export PATH=$PATH:${MYSQL_HOME}/bin

13、查看原始密码

[root@nat1 mysql5.7]# cat /root/.mysql_secret 
# Password set for user 'root@localhost' at 2019-11-14 05:20:44 
VAivyqww1:jg

14、登陆并注册一个新用户;远程登陆

[root@nat1 mysql5.7]# mysql -root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26

Copyright (c) 2000, 2019, 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('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit;
Bye
[root@nat1 mysql5.7]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 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> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
31 rows in set (0.00 sec)

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

mysql> create user 'admin'@'%' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

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

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

mysql> quit;
Bye
[root@nat1 mysql5.7]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@nat1 mysql5.7]# mysql -uadmin -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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.

15、测试远程连接

在windows下连接该mysql数据库。测试成功。注意用户名使用admIn,ip地址使用真实ip。

你可能感兴趣的:(linux)