首页 > 代码库 > LInux下安装Mysql

LInux下安装Mysql

1. Mysql下载:从官网下载需要的mysql安装包:https://www.mysql.com/downloads/ or http://dev.mysql.com/downloads/mysql/。选择Community→MYSQL Community Server(GPL)→Linux-Generic选择需要的版本即可,本次安装下载的版本为:mysql-test-5.7.17-linux-glibc2.5-x86_64.tar.gz

技术分享

2. 检查你所用的Linux下有没有安装过mysql,没有卸载干净
rpm -qa|grep -i mysql检查是否安装了mysql,有的话全部卸掉
rpm -e mysql-5.7.13-linux-glibc2.5-x86_64 --nodeps 强制卸除
3. 创建mysql的用户组/用户, data目录及其用户目录
在这步之前一定要先确保你所建的用户以及用户组没有存在,要不然在后面的过程中会报错,删除时候
要先删除用户在删除用户组名。
userdel mysql #删除用户
groupdel mysql #删除用户组名
mkdir /home/mysql #在home文件夹下创建mysql
groupadd mysql #创建一个名为mysql的用户组
useradd -g mysql -d /home/mysql mysql #在用户组下创建用户
4. 解压安装包并将解压包里的内容拷贝到mysql的安装目录/home/mysql
tar -zxvf XXX.tar.gz
cd XXX
mv * /home/mysql
5. 初始化mysql数据库
cd /home/mysql
5.7新特性需要运行:
运行./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data -- initialize报错:
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决办法: yum install libaio* 自动安装这两个包
其他版本运行:
./bin/mysqld_install_db --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data
安装成功后:
2017-02-17T08:36:05.914355Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-02-17T08:36:06.885084Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-02-17T08:36:07.022957Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2017-02-17T08:36:07.087779Z 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: 20305996-f4ec-11e6-90f4-005056878563.
2017-02-17T08:36:07.089250Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
2017-02-17T08:36:07.090938Z 1 [Note] A temporary password is generated for root@localhost: .vR8gaai!Z6e
6. 检查mysql服务是否可以启动:
cd /home/mysql
./supprot-files/mysql.server start
Starting Mysql...OK!
可能报错找不到路径,需要修改配置文件或者建立软连接
# ./support-files/mysql.server start
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn‘t find MySQL server (/usr/local/mysql/bin/mysqld_safe)
由上面可知mysql的tar.gz安装包的默认安装目录为/usr/local/mysql, 这时候我们需要修改/support-files/mysql.server文件的basedir和datadir目录路径为我们环境所在的mysql的basedir和datadir路径, 如下:
# vim support-files/mysql.server
--------------------------
...
basedir=/home/mysql
datadir=/home/mysql/data
...
--------------------------
# ./support-files/mysql.server start
start Starting MySQL.. OK!
创建软连接
# ln -s /home/mysql/bin/mysql /usr/bin/mysql
删除软连接:rm -rf mysql,不要带/,否则会将源文件删除掉
7. 创建配置文件:
将默认的my.cnf备份,如果没有则创建my.cnf
 mv /etc/my.cnf /etc/my.cnf.bak
将安装目录下的my-default.cnf拷贝到/etc下面
cd /home/mysql/support-files
cp my-default.cnf /etc/my.cnf
设置编码为utf8,以防止乱码
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
8. 配置mysql服务开机自动启动
# cp /home/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                                        # 用这个命令设置开机启动:
9. mysql服务启动/重启/停止
# service mysqld start                                               # 启动服务
# service mysqld restart                                             # 重启服务
# service mysqld stop                                                # 停止服务
10. 初始化mysql用户root的密码
# 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
--------------
11. 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>
grant all on *.* to ‘root‘@‘%‘ identified by ‘123456‘;
 
参考博文:http://www.cnblogs.com/gaojupeng/p/5727069.html
技术分享

LInux下安装Mysql