1、由于在线安装受制于网络环境,所以选择tar包编译安装。
首先去mysql镜像站下载mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2、上传到centos
3、检查你所用的Linux下有没有安装过mysql,没有卸载干净
rpm -qa|grep -i mysql
如果存在,则需要先卸载掉,不然会出现覆盖错误。
yum remove mysql mysql-server mysql-libs mysql-server;
find / -name mysql 将找到的相关东西delete掉;
rpm -qa|grep mysql(查询出来的东东yum remove掉)
4、创建mysql的用户组/用户, data目录及其用户目录
userdel mysql # 删除用户
groupdel mysql # 删除用户组名
mkdir /usr/local/mysql # mysql的默认安装路径,建议不要更换,如果更换后续需要更新配置
mkdir /usr/local/mysql/data # 在mysql文件夹下创建文件夹data
groupadd mysql # 创建一个名为mysql的用户组
useradd -g mysql -d /usr/local/mysql mysql # 在用户组下创建用户
5、解压缩并转移
# tar -xzvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz # 解压文件
# cd mysql-5.7.17-linux-glibc2.5-x86_64 # 进入
# mv * /usr/local/mysql/
6、编译安装
cd /usr/local/mysql/
./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
记录上面成功安装后的密码,如上:hIE;k,h8gd#q,后续用到!
7、启动mysql服务
./support-files/mysql.server start
截至目前,证明mysql已运行成功!!!
8、配置mysql
#进入mysql的安装目录支持文件目录
cd /usr/local/mysql//support-files
# 拷贝配置文件模板为新的mysql配置文件,
cp my-default.cnf /etc/my.cnf
#设置编码,可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行.
#如下是我修改配置文件/etc/my.cnf, 设置编码为utf8以防乱码
vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
character_set_server=utf8
init_connect='SET NAMES utf8'
[client]
default-character-set=utf8
此处需要注释掉##sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
否则后续登陆时会报异常:
mysql: [ERROR] unknown variable 'sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES'
9、配置mysql服务开机自动启动
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # 拷贝启动文件到/etc/init.d/下并重命令为mysqld
# chmod 755 /etc/init.d/mysqld # 增加执行权限
# chkconfig --list mysqld # 检查自启动项列表中没有mysqld这个,
# chkconfig --add mysqld # 如果没有就添加mysqld:
# chkconfig mysqld on # 用这个命令设置开机启动:
10、mysql服务的启动/重启/停止
# service mysqld start # 启动服务
# service mysqld restart # 重启服务
# service mysqld stop # 停止服务
11、修改mysql用户root的密码
mysql -u root -p
输入上边自动生成的密码,进入mysql环境
Mysql -u root -p
-bash : mysql :command not found
原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
首先得知道mysql命令完整路径,比如我的Linux的mysql的路径是:/usr/local/mysql/bin/mysql,我们则可以这样执行命令:
/usr/local/mysql/bin/mysql -uroot这样执行命令,或者创建一个软链接
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
再次尝试就可以进入了
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> 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> \s
--------------
mysql Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 5
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.17
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /tmp/mysql.sock
Uptime: 5 min 8 sec
Threads: 1 Questions: 44 Slow queries: 0 Opens: 136 Flush tables: 1 Open tables: 129 Queries per second avg: 0.142
--------------
mysql> show variables like 'character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)
mysql> SET character_set_database = utf8;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show variables like 'character%';
+--------------------------+----------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)
mysql> \s
--------------
mysql Ver 14.14 Distrib 5.7.17, for linux-glibc2.5 (x86_64) using EditLine wrapper
Connection id: 5
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.17
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: 6 min 20 sec
Threads: 1 Questions: 50 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.131
--------------
13、mysql远程授权
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
14、验证,可能需要关闭防火墙
Service iptables stop