设为首页 收藏本站
查看: 459|回复: 0

[经验分享] CentOS 7.0 安装 mysql-5.7.14

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-9-9 11:12:13 | 显示全部楼层 |阅读模式
初学linux 路上遇过各种坑 把正确安装mysql-5.7.14分享一下

1.CentOs7.0 默认的数据库为MariaDB,先卸载MariaDB,否则安装mysql,引起冲突

先查看当前安装的MariaDB
1
   
rpm -qa|grep mariadb

之后下面就会列出现在的版本 使用
1
   
rpm -e --nodeps mariadb........


2.之后 创建文件夹 我是放在/home/mysql
1
   
mkdir /home/mysql/data


3.创建mysql用户组与用户
1
2
   
groupadd mysql
useradd -g mysql  -d /home/mysql mysql

PS.如果已经存在提示错误了  可以先删除
1
2
   
userdel mysql                                      
groupdel mysql


4.mysql5.7之前使用
1
   
./bin/mysql_install_db --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data

命令

但是 5.7以后已经放弃mysql_install_db

所以使用新的
1
   
./bin/mysqld --user=mysql --basedir=/home/mysql --datadir=/home/mysql/data --initialize


这时有人会提示
1
   
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory


缺少libaio.so 文件  对此文件进行安装
1
2
   
yum search libaio  #查询文件
yum install libaio #安装文件


之后执行上面安装命令
1
2
3
4
5
6
   
2016-09-08T06:34:35.215617Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-09-08T06:34:37.175558Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-09-08T06:34:37.456478Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-09-08T06:34:37.529873Z 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: 505a95f7-758e-11e6-a966-00163e0368de.
2016-09-08T06:34:37.531971Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-09-08T06:34:37.534742Z 1 [Note] A temporary password is generated for root@localhost: b/:q5Dd&mo-v

牢记上面的随机密码, 如上b/:q5Dd&mo-v, 下面我们修改密码时需要用到。


PS.如果你的data文件有内容 会报

[ERROR] --initialize specified but the data directory has files in it. Aborting.

这个错误 正确的是把data文件清空 rm -rf *


5.之后我们运行一下

cd /home/mysql
/support-files/mysql.server start

之后你会收到如下错误
./support-files/mysql.server: line 276: cd: /usr/local/mysql: No such file or directory
Starting MySQLCouldn't find MySQL server (/usr/local/mysql/[FAILED]ld_safe)

因为默认的目录是/usr/local/mysql 我们换了别的目录  只做继续修改
vi support-files/mysql.server
按I进入编辑模式(这是新手的福音 当年我就不知道)
找到
basedir=/home/mysql
datadir=/home/mysql/data
进行修改成现在的目录 之后 ESC , :号后WQ回车 保存退出

继续运行 我还不信了 。。终于 成功了
Starting MySQL.                                            [  OK  ]
不容易啊。。。。 之后 我们继续

6.创建软链接 软链接就是两个地方都可以用一个文件 却不多占磁盘地方 在linux里还是很有用的
# ln -s /home/mysql/bin/mysql /usr/bin/mysql

7. 创建配置文件
将默认生成的my.cnf备份
# mv /etc/my.cnf /etc/my.cnf.bak

进入mysql的安装目录支持文件目录
# cd /home/mysql/support-files

拷贝配置文件模板为新的mysql配置文件,
# cp my-default.cnf /etc/my.cnf

可按需修改新的配置文件选项, 不修改配置选项, mysql则按默认配置参数运行.
如下是我修改配置文件/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服务开机自动启动

拷贝启动文件到/etc/init.d/下并重命令为mysqld
# cp /home/mysql/support-files/mysql.server /etc/init.d/mysqld

增加执行权限
# chmod 755 /etc/init.d/mysqld

检查自启动项列表中没有mysqld这个,如果没有就添加mysqld:
# chkconfig --list mysqld
# chkconfig --add mysqld

设置MySQL在345等级自动启动
# chkconfig --level 345 mysqld on

或用这个命令设置开机启动:
# chkconfig mysqld on


9. mysql服务的启动/重启/停止

启动mysql服务

# service mysqld start

重启mysql服务

# service mysqld restart

停止mysql服务

# service mysqld stop

10. 初始化mysql用户root的密码

先将mysql服务停止

# service mysqld stop

进入mysql安装目录, 执行:
1
2
3
4
5
6

cd /home/mysql
./bin/mysqld_safe --skip-grant-tables --skip-networking&

[1] 6225[iyunv@localhost mysql]# 151110 02:46:08 mysqld_safe Logging
to '/home/mysql/data/localhost.localdomain.err'.151110 02:46:08 mysqld_safe Starting mysqld
daemon with databases from /home/mysql/data12345

另外打开一个终端(p.s. 如果是ssh连接登录的, 另外创建一个ssh连接即可), 执行操作如下:
1
# mysql -u root mysql



1
2
3
4
5
6
7
8
9
10
11
12
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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.



1
# mysql> use mysql;



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
Database changed

# mysql> UPDATE user SET password=PASSWORD('123456') WHERE user='root';

ERROR 1054 (42S22): Unknown column 'password' in 'field list'

mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';

Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 1

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

mysql> \s
--------------
mysql  Ver 14.14 Distrib 5.7.9, for linux-glibc2.5 (x86_64) using  EditLine wrapper
Connection id:      2
Current database:   mysql
Current user:       root@
SSL:            Not in use
Current pager:      stdout
Using outfile:      ''
Using delimiter:    ;
Server version:     5.7.9 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:         4 min 47 sec
Threads: 1  Questions: 43  Slow queries: 0  Opens: 127  Flush tables: 1  Open tables: 122 Queries per second avg: 0.149--------------

mysql> exit;
Bye



到此, 设置完mysql用户root的密码且确保mysql编码集是utf8, 注意上面, 新版本的mysql.user表里的密码字段是authentication_string

快捷键ctrl + c停止# ./bin/mysqld_safe ...命令, 重新启动mysql服务, 用新密码连接mysql:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# service mysqld startStarting MySQL SUCCESS!
[iyunv@localhost bin]# mysql -uroot -pEnter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3Server version: 5.7.9Copyright (c) 2000,
2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates.
Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement
before executing this statement.
mysql > exit;
Bye



1
咦?又要我改密码, 我们通过mysqladmin来修改密码, 先输入原密码, 再设置新密码, 总算可以了吧!!!



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# cd /home/mysql# ./bin/mysqladmin -u root -p passwordEnter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to
ensure password safety.# mysql -uroot -pEnter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6Server version: 5.7.9 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates.
Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

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>



1
或直接:



1
# ./bin/mysqladmin -uroot -p 'b/:q5Dd&mo-v' 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.1234
其中,
1
b/:q5Dd&mo-v



就是我们在使用mysqld --initialize时牢记下的随机密码

11. mysql远程授权

格式如下:

1
mysql> grant all [privileges] on db_name.table_name to 'username'@'host' identified by 'password';



示例如下:

1
2
3
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';

Query OK, 0 rows affected, 1 warning (0.04 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)mysql> 12345678



或用

1
mysql> grant all on *.* to 'root'@'%' identified by '123456';



打完收功!




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-269918-1-1.html 上篇帖子: Python3连接MySql 下篇帖子: mysqldump 备份数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表