您的位置:首页 > 数据库 > MySQL

Mysql-01.安装和配置(重新整理)

2016-11-08 21:36 489 查看
Windows操作系统下安装和配置

一、Mysql安装

Window系统下Mysql安装文件分为msi和zip格式,其中msi格式可以直接点击安装,按照安装提示进行安装配置即可,zip格式的则需要解压,解压完毕后需要手工配置才可以正常运行。

二、Mysql配置

ZIP版本的Mysql下载到本地后,直接解压即安装完毕。如解压路径为D:\Program Files\MySql,则Msql的配置和启动路径就在D:\Program Files\MySql\bin目录下。

配置默认的环境变量:将D:\Program Files\MySql\bin加入系统的环境变量Path中;

1.修改mysql默认配制信息

将my-default.ini修改为my.ini,修改my.ini内容,添加如下信息

basedir=D:\Program Files\MySql //Mysql程序目录

datadir=D:\Program Files\MySql\data //数据库存放目录

port=3306 //端口号

[mysql] default-character-set=utf8 //默认字符集(和下面的不一样)

[mysqld] character-set-server=utf8 //默认字符集(和上面的有区别)

2.安装mysql服务

用管理员身份启动命令行窗口(cmd以管理员身份启动),进入D:\Program Files\MySql\bin目录下,执行安装mysql服务指令:mysqld install mysql,即可安装mysql服务。安装成功后,在Windows的服务里面就会看到mysql服务(还未启动)

说明: ①mysqld install mysql 前面的是安装指令,后面的是服务名,默认服务名为Mysql;

②也可以安装的时候制定配置文件,mysqld -install mysql --defaults-file="C:\MySQL\my.ini"。

注意点:①一定要以管理员身份启动cmd命令行操作窗口;

②必须到Mysql安装目录的bin目录下执行安装服务的指令;

③安装之前,查看一下有没有data目录,环境变量datadir配置的目录下;

3.启动/停止mysql服务

启动mysql服务

执行net start mysq命令启动服务,则会自动初始化,有可能配置成功,但大多数情况下可能会出现服务无法启动,没关系,使初始化命令进行初始化。

初使化之前请清空D:\MySQL_Data内的文件,执行初使化命令:

mysqld --initialize --user=mysql --console

执行完后cmd控制台上会显示一大堆英文,大概是说配置文件中缺少参数。后期修改my.ini即可。

说明:首次安装并启动mysql经常会出问题,大概原因是mysql的data目录中没有初始化数据导致,按照上述操作执行初始化之后,data目录中会自动生成初始化数据,后续再执行启动服务操作基本上就不会再报错了。

启动服务:net start mysql,服务成功启动。

停止mysql服务

当需要停止mysql服务的之后,执行net stop mysql,即可停止名为mysql的服务。

4.卸载mysql服务

执行mysqld remove mysql,即可删除名为mysql的服务。

三、.Mysql启动问题

1.Mysql启动后报MySQL ERROR 1045 : Access denied for user 'ODBC'@'localhost' (using password: NO)错误原因以及解决办法。

原因:

由于MySQL5.7在安装完后,第一次启动时,会在root目录下生产一个随机的密码,文件名为 .mysql_secret。所以,登录时需要用随机密码登录,然后通过“SET PASSWORD = PASSWORD(‘new password’);”命令修改密码后才能正常访问。如果忘记修改密码,再次登录Mysql的时候,后台即会报上述错误。

window平台下解决解决办法:

1)关闭正在运行的服务;net stop mysql

2)管理员用户打开一个cmd窗口,切换到mysql的bin目录,运行如下命令

mysqld --defaults-file="C:\Program Files\MySQL\my.ini" --console --skip-grant-tables

释:该命令通过跳过权限安全检查,开启mysql服务,这样连接mysql时,可以不用输入用户密码。此时这个命令行窗口就被锁定,不再继续执行。

3)管理员用户再打开另外一个cmd窗口,输入mysql或者mysql -uroot -p,成功进入数据库,此时命令行开头变为mysql>;

4)输入show databases;命令,命令行会展示当前所有的数据库;使用use mysql 切换到mysql数据库;

5)使用UPDATE user SET Password=PASSWORD('newpassword') where USER='root';命令修改mysql数据库的root用户密码;(Windows系统的mysql的root用户密码是123456)

6)用FLUSH PRIVILEGES;命令刷新权限,然后执行quit命令后退出;

7)重新使用mysql -u root -p登录,提示输入密码,输入新密码即可登录成功。

备注:

1)解决办法中4和5可以合并,执行UPDATE mysql.user SET Password = PASSWORD ('newpassword') where USER='root';即可修改mysql数据库的root用户命令。

2)执行5中的update语句时,可能会报ERROR 1054 (42S22): Unknown column 'password' in 'field list'错误,因为mysql 5.7版本数据库下没有Password字段了,而改为authentication_string,此时只需要把update语句中的Password替换为authentication_string即可。

修改密码的SQL指令:SET PASSWORD = PASSWORD('123456');

创建数据库并制定字符集:CREATE DATABASE JForum DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

设置数据库权限,并为制定用户设置密码:grant select,insert,update,delete,create,drop on JForum.* to admin@localhost identified by '123456';

-----------------------------------------------------------------------------------------------------------

Linux操作系统下安装和配置

一、Mysql安装

操作系统选择CentOS 7,CentOS 7的yum源中没有正常安装MySQL时的mysql-sever文件,需要去官网上下载(电脑要联网啊,不然就只有把包上传上去了)

[root@promote local]# wget http://repo.mysql.com//mysql57-community-release-el7-9.noarch.rpm

[root@promote local]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm

[root@promote local]# yum install mysql-community-server

安装完毕后,密码修改有些问题,参照:

二、Mysql修改密码步骤

/*Linux安装好mysql后,用root用户登录*/

[root@promote test]# mysql -u root -p

Enter password: ——随便输入一个密码,报错

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

/*查看mysql安装log中生成的连接密码*/

[root@promote test]# cat /var/log/mysqld.log |grep password

2016-11-17T15:33:37.276548Z 1 [Note] A temporary password is generated for root@localhost: Z%Vxqbcw:8z+ ——此为mysql安装时的随机密码

[root@promote test]# mysql -u root -p

Enter password: ——输入随机密码,登录成功

Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 7

Server version: 5.7.16

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> ——出现mysql>即表示连接成功

mysql> show databases; ——查看数据库信息

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. ——提示需要更新密码

mysql>

/*更新密码操作步骤——重点啊*/

[root@promote test]# vi /etc/my.cnf

/*编辑my.cnf文件,在[mysqld]下第一行加入skip-grant-tables,代表不实用密码登录,保存退出*/

[root@promote test]# service mysqld restart ——重启mysql服务,是mysqld

[root@promote test]# mysql -u root ——无密码登录

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.16 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>

/*无密码登录成功后执行修改密码指令,mysql.user中的mysql为数据库名*/

mysql> update mysql.user set authentication_string = password('new_password'), password_expired = 'N', password_last_changed = now() where user = 'root';

Query OK, 1 row affected, 1 warning (0.00 sec)

Rows matched: 1 Changed: 1 Warnings: 1

mysql> FLUSH PRIVILEGES; ——刷新权限

Query OK,1 rows affected (0.00 sec)

mysql> quit ——退出mysql后

Bye

[root@promote test]# vi /etc/my.cnf ——修改my.cnf文件,删除增加的记录

[root@promote test]# service mysqld restart ——重启mysql服务

Redirecting to /bin/systemctl restart mysqld.service

[root@promote test]# ——重启成功

[root@promote test]# mysql -u root ——无密码启动,启动报错

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@promote test]# 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.16 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>

三、修改Mysql的默认字符集

分别执行show variables like 'character%';和show variables like 'collation%';指令,查看mysql默认安装的字符集;如不是希望的字符集的话,修改my.cnf,设置字符集后重启mysql即可。

/*修改步骤如下*/

[root@promote test]# vi /etc/my.cnf

【编辑my.cnf文件在[mysqld]最下面增加一行信息:character-set-server=utf8,保存编辑】

[root@promote test]# service mysqld restart ——重启mysql服务

Redirecting to /bin/systemctl restart mysqld.service

[root@promote test]# mysql -u root -p ——用户登录后查看字符集信息

Enter password: &nb
3ff0
sp; ——输入密码登录成功,隐去了提示信息

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/share/mysql/charsets/ |

+--------------------------+----------------------------+

8 rows in set (0.00 sec)

mysql> show variables like 'collation%';

+----------------------+-----------------+

| Variable_name | Value |

+----------------------+-----------------+

| collation_connection | utf8_general_ci |

| collation_database | utf8_general_ci |

| collation_server | utf8_general_ci |

+----------------------+-----------------+

3 rows in set (0.00 sec)

mysql>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: