LInux下安装Mysql
Posted 麦兜日常学习笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
--------------
可能会出现密码错误登陆不进去的情况:
1)使用service mysqld stop停止服务,然后使用./home/mysql/bin/mysqld_safe --skip-grant-tables &启动mysql服务,
2)输入 ./home/mysql/bin/mysql -u root -p 回车进入
>use mysql;
update mysql.user set authentication_string=password(\'123456\') where user=\'root\';
> flush privileges; 更新权限
> quit 退出
11. mysql远程授权
重新启动mysql服务,使用修改后的密码登陆,有可能进去mysql后提示ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.需要执行下
SET PASSWORD = PASSWORD(\'123456\');问题解决
[root@testfornss ~]# mysql -u root -p
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
http://blog.csdn.net/zl17/article/details/8589454
以上是关于LInux下安装Mysql的主要内容,如果未能解决你的问题,请参考以下文章