Linux下MySQL安装部署

一、版本下载

路径:https://www.mysql.com/downloads/,选择最下方的下载链接进入

目前已经到8.0.15版本了,但是在公司里大部分都使用5.x版本,从5.4以后MySQL整合了三方公司的新存储引擎,推荐使用5.5或5.7 ,这里以5.5版本为例。linux下安装MySQL有三种方式yum、tar.gz、rpm,这里选择rpm方式安装。

查看linux系统是32还是64位的,选择对应的rpm包(目前环境基本是64位)

# uname -a
Linux dap48 2.6.32.12-0.7-default #1 SMP 2010-05-20 11:14:20 +0200 x86_64 x86_64 x86_64 GNU/Linux

下载MySQL-server-5.5.62-1.el7.x86_64.rpm和MySQL-client-5.5.62-1.el7.x86_64.rpm。

二、安装前检查

安装MySQL数据库之前,需要保证所安装的机器没有MySQL数据库相关文件。

1、查找以前是否装有mysql

命令:rpm -qa|grep -i mysql
可以看到如下图的所示:

说明之前安装了:
MySQL-client-5.5.25a-1.rhel5
MySQL-server-5.5.25a-1.rhel5

2、停止mysql服务、删除之前安装的mysql

删除命令:rpm -e --nodeps 包名
# rpm -e --nodeps MySQL-client-5.5.25a-1.rhel5
# rpm -e --nodeps MySQL-server-5.5.25a-1.rhel5

3、查找之前老版本mysql的目录、并且删除老版本mysql的文件和库

[root@localhost ~]# find / -name mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/lib64/mysql

删除对应的mysql目录
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql
rm -rf /usr/lib64/mysql

注意:卸载后/etc/my.cnf不会删除,需要进行手工删除
 rm -rf /etc/my.cnf

4、再次查找机器是否安装mysql

rpm -qa|grep -i mysql
无结果,说明已经卸载彻底、接下来直接安装mysql即可

三、安装步骤

1、FTP软件,二进制方式上传安装包到机器,本例拷贝到/home/version下,执行rpm命令进行Mysql的安装:

备注:服务端和客户端2个包都需要安装。

# rpm -ivh MySQL-server-5.5.62-1.el7.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:MySQL-server           ########################################### [100%]

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h zdh90 password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

# rpm -ivh MySQL-client-5.5.62-1.el7.x86_64.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]

2、使用root用户把Mysql启动

# /etc/rc.d/init.d/mysql start(或者mysql service start)
Starting MySQL..  

验证mysql是否成功,命令:mysqladmin --version

 其他相关命令:启动 service mysql start,   关闭 service mysql stop,   重启 service mysql restart

3、Mysql连接测试,登录mysql(密码不用输入,因为root初始密码为空):

[root@zdh90 version]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> Ctrl-C -- exit!
Aborted

4、安装后MySQL路径查看

Mysql安装后存在如下相关目录:
(1)、数据库目录 
   /var/lib/mysql/ 
(2)、配置文件 
   /usr/share/mysql(mysql.server命令及配置文件) 
(3)、相关命令 
   /usr/bin(mysqladmin mysqldump,mysql等命令) 
(4)、启动脚本 
   /etc/rc.d/init.d/(启动脚本文件mysql的目录)
另外,查看mysql的安装路径,命令:ps -ef|grep mysql,其中datadir表示数据库目录,pid-file表示pid文件目录
其中,MySQL配置文件:
my-huge.cnf 高端服务器 1-2G内存
my-large.cnf等待
但是,这些配置文件mysql默认不能识别,默认只能识别/etc/my.cnf
注意:mysql5.5默认配置文件/etc/my.cnf,Mysql5.6默认皮质文件/etc/mysql-default.cnf

5、修改数据库目录

目录确认拷贝无误,并且后面正常运行后,把原来目录下/var/lib/mysql目录删除

首先创建用户数据目录:
# cd /home
# mkdir data
停止mysql进程:
# /etc/rc.d/init.d/mysql stop
Shutting down MySQL.                                       [确定]
把/var/lib/mysql整个目录移到/home/data:
# cp -R /var/lib/mysql/ /home/data

 6、给新目录下的的用户和用户组改变用户和用户组信息,如下:

#cd /home/data
#chown -R mysql:mysql mysql

7、配置/etc/my.cnf文件,如果该文件不存在直接新增一个配置文件。按照下面规整修改配置文件,修改完毕后保存。

备注:这个配置文件修改之后,一定要more命令打开检查是否有中文错乱(可以清理后面的中文解析)。文件内容参考如下:

# vi my.cnf

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
#socket         = /var/lib/mysql/mysql.sock     #----将原来的配置注释掉
socket         = /home/data/mysql/mysql.sock  # ----添加到新的位置


# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
#socket         = /var/lib/mysql/mysql.sock  #----将原来的配置注释掉
socket          = /home/data/mysql/mysql.sock #----添加到新的位置

skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
#add by manual                            #----以下为手动添加character_set_server=utf8                 #   ----修改默认字符集设置
max_connections=300                       #----设置最大连接数,默认为 151,MySQL服务器允许的最大连接数16384
lower_case_table_names = 1   #----设定数据库名和表名大小写不敏感
max_connect_errors = 100000  #----设定连接错误为一个较大值
innodb_file_format = barracuda # ----接着的3个配置项目解决主键不能超过767 bytes的限制
innodb_file_per_table = true
innodb_large_prefix = true
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default-character-set = utf8                   #----修改默认字符集设置

8、修改mysql的启动脚本中的datadir目录,原来为空修改为新的目录:

# vi /etc/rc.d/init.d/mysql

basedir=
datadir=/home/data/mysql  

9、重新启动mysql服务,如下:

# /etc/rc.d/init.d/mysql start
Starting MySQL..                                           [确定]

10、删除匿名用户,如下: 

查看用户信息,如下:
# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
|      | localhost |          |
+------+-----------+----------+
6 rows in set (0.00 sec)
执行删除命令,如下:
mysql> delete from mysql.user where user = '';
Query OK, 2 rows affected (0.00 sec)

mysql>  select user,host,password from mysql.user;--检查是否删除成功。
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
+------+-----------+----------+
4 rows in set (0.00 sec)

11、给Mysql设置密码,

方法一:命令/usr/bin/mysqladmin -u root  password 'new password'

登录Mysql: mysql -u root -p

方法二:

mysql> update mysql.user set password=password("root") where user="root";
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0
设置'root'用户对应的密码,后面的PASSWORD('root')表明调用密码函数用于密码加密。这一条语句执行完毕之后,上面表中的4条记录都被修改了。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
必须让服务器用FLUSH PRIVILEGES重新读授权表。否则,重新启动服务器前,不会使用更改。
mysql> select user,host,password from mysql.user;--检查是否正常加密
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1       | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)

12、修改mysql的root用户登录权限,如下:

上述11步的操作结束之后,root用户能从本机登录,但是无法在其他机器通过IP登录,所以还需要执行下面语句,用以表明root用户可以在任意IP地址进行登录:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

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

mysql> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1       | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | %         | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
+------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye

13、使用密码登录mysql,如下:

# mysql -u root -p
Enter password: root  ----输入root用户的密码root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.36-log MySQL Community Server (GPL)

Copyright (c) 2000, 2014, 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> exit
Bye

14、MySQL编码,使用mysql命令查看:show variables like 'char';可以发现部分编码是latin,需要统一设置为utf-8。

设置编码:vi /etc/my.cnf

[client]

default-character-set=utf8

[mysqld]

default-character-set=utf8

[mysql]

character_set_server=utf8

character_set_client=utf8

collation_server=utf8_general_ci

重启mysql生效,注意修改编码只对之后创建的数据库生效,因此建议在mysql安装完毕后第一时间改编码

默认端口3306

15、常见清屏命令:ctrl+L, system clear

四、注意事项

1、如果提示GPG keys....安装失败,在安装命令后加 --force --nodoeps

2、在计算机reboot后,登录MySQL,可能会报错“/var/lib/mysql/mysql.sock不存在”,原因是Mysql服务没有启动。

启动服务:1、每次使用前 手动启动服务  /etc/init.d/mysql start

                  2、开机自启 chkconfig mysql on,,关闭自启chkconfig mysql off

检查开机是否自动启动:ntsysv

原创文章 33 获赞 6 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lanyue1/article/details/87987803