1. 从官网下载 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz

经测试, 本文还适用于如下版本: 

MySQL-5.7.10-Linux-glibc2.5-x86_64.tar.gz 

mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz

mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz

mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz

官网: http://dev.mysql.com/downloads/mysql/

2.检查你所用的Linux下有没有安装过mysql,没有卸载干净

#rpm -qa|grep -i mysql
mysql-5.7.13-linux-glibc2.5-x86_64

3. 创建mysql的用户组/用户, data目录及其用户目录

# userdel mysql                                           # 删除用户
# groupdel mysql                                          # 删除用户组名
# mkdir /home/mysql                                       # 在home文件夹下创建文件夹mysql
# mkdir /home/mysql/data                                  # 在mysql文件夹下创建文件夹data  
# groupadd mysql                                          # 创建一个名为mysql的用户组       
# useradd -g mysql -d /home/mysql mysql                   # 在用户组下创建用户

4、解压安装包并将解压包里的内容拷贝到mysql的安装目录/home/mysql

# tar -xzvf mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz              # 解压文件
# cd mysql-5.7.13-linux-glibc2.5-x86_64                            # 进入
# mv * /home/mysql                                                # 移动到之前我创建的mysql文件夹下

# cd /home/mysql   

# ./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize

牢记上面的随机密码, 如上**mjT,#x_5sW, 下面我们修改密码时需要用到。

6、检测下是否能启动mysql服务

  

# cd /home/mysql
# ./support-files/mysql.server start
Starting MySQL.. OK!



# vim support-files/mysql.server
--------------------------
...
basedir=/home/mysql
datadir=/home/mysql/data
...
--------------------------
# ./support-files/mysql.server
start Starting MySQL.. OK!
注意:上面 basedir 以及datadir部分是手动改的,那么在命令中如何手动改,步骤
1、 按 键盘 i 键(insert),然后就可以在你想要的位置编写了。
2、编辑完毕退出,esc键。
3、Shift键 加 :(冒号键)退出编辑。
4、wq!   保存编辑



# ln -s /home/mysql/bin/mysql /usr/bin/mysql


# mv /etc/my.cnf /etc/my.cnf.bak


cd /home/mysql/support-files


# cp my-default.cnf /etc/my.cnf


# vim /etc/my.cnf
 
[mysqld]
 
basedir = /home/mysql
datadir = /home/mysql/data
 
character_set_server=utf8
init_connect='SET NAMES utf8'
  
[client]
default-character-set=utf8

mysql服务的启动/重启/停止


# service mysqld start                                               # 启动服务
# service mysqld restart                                             # 重启服务
# service mysqld stop       
# cd /home/mysql
# ./bin/mysqladmin -u root -p '**mjT,#x_5sW' password '123456' mysqladmin:                                         # 刚刚之前的初始密码
[Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
mysql> use mysql
Database changed
mysql> update user set authentication_string = PASSWORD('123456'where user 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.13, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id:  3
Current database:   mysql
Current user:   root@localhost
SSL:    Not in use
Current pager:  stdout
Using outfile:  ''
Using delimiter:    ;
Server version: 5.7.13 MySQL Community Server (GPL)
Protocol version:   10
Connection: Localhost via UNIX socket
Server characterset:    utf8
Db characterset:    utf8
Client characterset:    utf8
Conn. characterset: utf8
UNIX socket:    /tmp/mysql.sock
Uptime: 1 hour 29 min 17 sec
 
Threads: 1 Questions: 50 Slow queries: 0 Opens: 136 Flush tables: 1 Open tables: 129 Queries per second avg: 0.009
--------------


这里教大家用另外一种方式修改密码,这个是在已经登陆进去的情况下依次执行。

mysql> SET PASSWORD PASSWORD('123456');             # PASSWORD()里面的123456 是我设置的新密码,你也可以设置成你的密码
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 1

12. mysql远程授权

[root@testfornss ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with or \g.
Your MySQL connection id is 4
Server version: 5.7.13 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql>

http://blog.csdn.net/cz596738622/article/details/53434790

第二种方式:

1、添加mysq组和用户

groupadd mysql

useradd -r -g mysql mysql

2、解压mysql安装包(mysql-5.7.18-linux-glibc2.5-x86_64.tar)

tar -xvfmysql-5.7.18-linux-glibc2.5-x86_64.tar

tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

3、重命名

mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql

4、更改mysql文件夹所属的组合用户

chown -R mysql /usr/local/mysql/

chgrp -R mysql /usr/local/mysql/

5、进入mysql目录 初始化mysql

bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

6、配置mysql

vi /etc/my.cnf 将附件里的内容复制粘贴进去

附件内容如下:

[mysqld]
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=utf8
skip-grant-tables
 
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
 
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
 
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6
  
server_id=1
 
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
  
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on
 
[mysqldump]
quick
 
max_allowed_packet = 32M
 
[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates
 
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
 
[mysqlhotcopy]
interactive-timeout
 
[mysqld_safe]
open-files-limit = 8192

注意事项:

basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
socket = /tmp/mysql.sock


这几个目录或文件如果不存在,你需要主动创建好,否则会报错。

这个配置文件中有这么一句话skip-grant-tables 是跳过权限和密码验证的;等后面修改完root密码时,需要把这句话注释掉。

7、mysql命令

cp -a /usr/local/mysql/suport-files/mysql.server /etc/init.d/mysql

service mysql start 启动服务

service mysql status 查看mysql状态

service mysql stop 停止服务

service mysql restart 重启服务

8、启动服务 登入系统

./usr/local/mysql/bin/mysql -uroot -p

9、修改root用户密码

//查看所有用户 use mysql; select * from user \G;

UPDATE mysql.user SET authentication_string = PASSWORD('mypassword') WHERE User = 'root' AND Host = 'localhost';

FLUSH PRIVILEGES;

10、退出mysql

exit