MySQL下载官网:
https://downloads.mysql.com/archives/community/
文章使用的MySQL版本下载地址:
https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
查询MySQL的安装文件:
[root@www ~]# find / -name mysql
/usr/local/env/mysql
/usr/local/env/mysql/mysql
/usr/local/env/mysql/mysql/bin/mysql
/usr/local/env/mysql/mysql/include/mysql
/usr/local/env/mysql/mysql/data/mysql
[root@www ~]#
删除:
[root@www ~]# rm -rf /usr/local/env/mysql
删除/etc/my.cnf文件:
[root@www ~]# rm -rf /etc/my.cnf
删除/etc/init.d/下跟MySQL有关的全部文件,通常包括mysql文件或者mysqld文件。/etc/init.d/目录下存放的一般都是加入系统的各个服务。
[root@www ~]# rm -rf /etc/init.d/mysql
[root@www ~]# rm -rf /etc/init.d/mysqld
[root@www ~]# userdel mysql
userdel: user 'mysql' does not exist
至此,卸载完成!
安装包下载地址:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
将下载好的mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz上传到Linux服务器并解压,笔者放到/usr/local/env/mysql目录下。
[root@www ~]# cd /usr/local/env/mysql/
[root@www mysql]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
重命名为mysql,并进入目录:
[root@www mysql]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql
[root@www mysql]# cd mysql
[root@www mysql]# groupadd mysql
[root@www mysql]# useradd -r -g mysql mysql
注:
查看用户以及用户组信息:
cat /etc/passwd # 查看所有用户信息
cat /etc/group # 查看所有组信息
groups 用户名 # 查看当前用户所在组,以及组内其它成员的信息
[root@www mysql]# rpm -qa | grep libaio
如果没有安装,使用如下命令安装:
[root@www mysql]# yum search libaio
[root@www mysql]# yum install libaio
[root@www mysql]# touch /etc/my.cnf
注意: 使用默认的644权限和用户,不要做修改。
将如下内容拷贝到里面:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
# default-character-set=utf8mb4
socket=/usr/local/env/mysql/mysql/lib/mysql/mysql.sock
[mysqld]
# 禁用DNS解析,一般不用
#skip-name-resolve
# 设置3306端口
port=3306
# 这里的socket要和[mysql]中的socket位置一致
socket=/usr/local/env/mysql/mysql/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/env/mysql/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/env/mysql/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
collation-server=utf8_bin
# 如果使用utf8mb4字符集就做如下两个配置
# character-set-server=utf8mb4
# collation-server=utf8mb4_unicode_ci
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 1表示数据库中的表名忽略大小写,0表示严格区分大小写
# lower_case_table_names=1
max_allowed_packet=16M
# 慢查询设置
# 是指执行超过多久的sql会被log下来,这里是2秒
# long_query_time =2
# 将查询返回较慢的语句进行记录
# log-slow-queries=/usr/local/env/mysql/mysql/logs/slow_query.log
# 就是字面意思,log下来没有使用索引的query
# log-queries-not-using-indexes=/usr/local/env/mysql/mysql/logs/no_use_index.log
# 对所有执行语句进行记录
# log=/usr/local/env/mysql/mysql/logs/mylog.log
[mysqld_safe]
log-error=/usr/local/env/mysql/mysql/logs/error.log
pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid
[root@www mysql]# mkdir /usr/local/env/mysql/mysql/lib/mysql
[root@www mysql]# mkdir /usr/local/env/mysql/mysql/logs
[root@www mysql]# mkdir /usr/local/env/mysql/mysql/data
在logs目录下创建error.log文件,否则有可能在初始化MySQL的时候会报错。
如果没有mysqld.pid文件,在data目录下建一个mysqld.pid文件,方便起见,可将其权限设置为777:
[root@www mysql]# cd /usr/local/env/mysql/mysql/
[root@www mysql]# touch logs/error.log
[root@www mysql]# touch data/mysqld.pid
[root@www mysql]# chmod 777 mysqld.pid
[root@www mysql]# chown -R mysql.mysql /usr/local/env/mysql/mysql
或
[root@www mysql]# chown -R mysql:mysql /usr/local/env/mysql/mysql
注:
[root@www ~]# cd /usr/local/env/mysql/mysql
[root@www mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/env/mysql/mysql --datadir=/usr/local/env/mysql/mysql/data
此时,控制台打印如下:
2018-10-24T04:25:13.673365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-24T04:25:15.808961Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-24T04:25:16.105505Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-24T04:25:16.184776Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cec94f21-d744-11e8-a0b5-fa163ed8e403.
2018-10-24T04:25:16.188372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-24T04:25:16.189074Z 1 [Note] A temporary password is generated for root@localhost: i;lknXwO;5,s
[root@www mysql]#
最后:i;lknXwO;5,s 就是初始化之后的密码。
[root@www mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@www mysql]# chmod +x /etc/rc.d/init.d/mysqld
修改 mysqld 文件,一定要修改:
[root@www mysql]# vim /etc/init.d/mysqld
因为在执行 systemctl start mysqld
的时候,执行参数 start 里面运行的是 $bindir 目录下的 /mysqld_safe 程序,而 mysqld_safe 程序是用来开启MySQL守护进程的程序,由于没有给它分配用户,所以 mysqld_safe 目前还是没有权限的状态,在守护进程没有权限的情况下直接启动MySQL必然会报错,所以要给 mysqld_safe 分配用户,用以授权。
在 $bindir/mysqld_safe 后面加上 --user=mysql ,MySQL目录的权限是什么用户就加什么用户,如果是 root 用户,就改成 --user=root ,修改后的文件如下:
修改完成后,保存并退出。
[root@www mysql]# chkconfig --add mysqld
[root@www 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
得到以上结果说明生效。
注:
启动mysqld:
[root@www mysql]# service mysqld start
Starting MySQL. SUCCESS!
设置开机启动:
[root@www mysql]# chkconfig mysqld on # 设置开机启动
[root@www mysql]# chkconfig mysqld off # 关闭开机启动
systemctl enable mysqld
,systemctl 命令只能操作正常安装在系统的本地服务,所以要通过 chkconfig 命令来实现开机启动。通过命令查看MySQL进程,会看到MySQL已经开启了守护进程:
[root@www mysql]# ps -ef|grep mysql
root 10569 1 0 Jul15 ? 00:00:00 /bin/sh /usr/local/env/mysql/mysql/bin/mysqld_safe --datadir=/usr/local/env/mysql/mysql/data --pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid
mysql 10974 10569 4 Jul15 ? 00:00:00 /usr/local/env/mysql/mysql/bin/mysqld --basedir=/usr/local/env/mysql/mysql --datadir=/usr/local/env/mysql/mysql/data --plugin-dir=/usr/local/env/mysql/mysql/lib/plugin --user=mysql --log-error=/usr/local/env/mysql/mysql/logs/error.log --pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid --socket=/var/lib/mysql/mysql.sock --port=3306
root 21543 10404 0 11:56 pts/0 00:00:00 grep --color=auto mysql
其中,进程号为10569的就是守护进程,有了守护进程,即便mysql的进程被杀掉了,守护进程也会立刻将mysql进程再次开启。
若要关闭MySQL,执行如下命令:
[root@www mysql]# service mysqld stop
# mysql环境变量
PATH=$PATH:/usr/local/env/mysql/mysql/bin
export PATH
或:
export PATH=$PATH:/usr/local/env/mysql/mysql/bin
[root@www mysql]# source /etc/profile
[root@www mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/root/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/usr/local/env/mysql/mysql/bin
出现了 /usr/local/env/mysql/mysql/bin 表示以已经配置成功。
第一次登陆的时候,使用初始化的密码:i;lknXwO;5,s
[root@www mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.21
Copyright (c) 2000, 2017, 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>
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
或者:
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 authentication_string=PASSWORD('123456') where User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges;
Query OK, 1 rows affected (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
注意:
# 允许所有用户远程访问 修改用户名和密码为你自己的
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
# 允许单个ip访问 修改用户名和密码为你自己的
mysql> grant all privileges on *.* to 'root'@'1.2.3.4' identified by 'password' with grant option;
# 最后
mysql> FLUSH PRIVILEGES;
此时可以查看user表中多了一个 host 为 % 的用户:
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> select user, host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql>
此时,可以使用navicat进行远程连接,或者在另外一台Linux服务器上用命令远程连接,假设MySQL所在服务器地址为 192.168.137.10:
[root@www ~]# mysql -h 192.168.137.10 -u root -p
Enter password:
输入密码即可。
如果使用navicate进行远程连接访问,报如下错误:
1045 - Access denied for user 'root'@'192.168.137.10' (using password: YES)
解决方法如下:
[root@www mysql]# service mysqld stop
Redirecting to /bin/systemctl stop mysql.service
在my.cnf配置文件添加如下内容:
# mysql无密码登录
skip-grant-tables
[root@www mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysql.service
注意:此时登录不带p参数:
[root@www mysql]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
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 mysql.user set authentication_string=password('123456') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 1
mysql>
mysql> quit
Bye
[root@www mysql]# service mysqld restart
Redirecting to /bin/systemctl restart mysql.service
[root@www mysql]#
再次使用navicat连接即可。
修改 /etc/my.cnf 配置文件,在 [mysqld] 节点下添加如下配置:
[mysqld]
# 开启binlog日志
# log_bin=ON
# binlog日志的基本文件名,后面会追加标识来表示每一个文件
# log_bin_basename=/usr/local/env/mysql/mysql/logs/mysql-bin
# binlog文件的索引文件,这个文件管理了所有的binlog文件的目录
# log_bin_index=/usr/local/env/mysql/mysql/mysql.index
# 设置日志路径,注意路径需要mysql用户有权限写。这一个参数和上面的三个参数是相同的,mysql会根据这个配置自动 设置 log_bin 为 ON 状态,自动设置 log_bin_index 文件名为你指定的文件名后跟 .index
log-bin=/usr/local/env/mysql/mysql/logs/mysql-binlog.log
# 指定当前mysql服务器的服务ID,为了区别集群内的其他mysql服务器。该参数5.7版本以下不需要设置,5.7以上的版本都要进行设置服务ID
server-id=002
# 设置日志格式,有三种:statement、row、mixed
binlog_format=mixed
# 设置binlog清理时间
expire_logs_days=7
# binlog每个日志文件的大小
max_binlog_size=100m
# binlog缓存大小
binlog_cache_size=4m
# 最大binlog缓存大小
max_binlog_cache_size=512m
# 无论是增量备份还是主从复制,都是需要开启mysql-binlog日志,最好跟数据目录设置到不同的>磁盘分区,可以降低io等待,提升性能;并且在磁盘故障的时候可以利用mysql-binlog恢复数据。
配置好之后,启动mysql,登陆。查看配置是否起作用:
mysql> show variables like '%log_bin%';
显示如下信息(log_bin的值为on),即表示设置成功。
+---------------------------------+----------------------------------------------------+
| Variable_name | Value |
+---------------------------------+----------------------------------------------------+
| log_bin | ON |
| log_bin_basename | /usr/local/env/mysql/mysql/logs/mysql-binlog |
| log_bin_index | /usr/local/env/mysql/mysql/logs/mysql-binlog.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
| sql_log_bin | ON |
+---------------------------------+----------------------------------------------------+
6 rows in set (0.00 sec)
十步杀一人,千里不留行
事了拂衣去,深藏身与名
–end–