Skip to content

Commit 2f84939

Browse files
committed
Merge pull request #417 from ijcd/mysql_option_secure_auth
Add secure_auth mysql_option()
2 parents 7ca5f1f + ecb675b commit 2f84939

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ Mysql2::Client.new(
108108
:connect_timeout = seconds,
109109
:reconnect = true/false,
110110
:local_infile = true/false,
111+
:secure_auth = true/false
111112
)
112113
```
114+
### Multiple result sets
113115

114116
You can also retrieve multiple result sets. For this to work you need to connect with
115117
flags `Mysql2::Client::MULTI_STATEMENTS`. Using multiple result sets is normally used
@@ -127,6 +129,22 @@ end
127129

128130
See https://gist.github.com/1367987 for using MULTI_STATEMENTS with Active Record.
129131

132+
### Secure auth
133+
134+
Starting wih MySQL 5.6.5, secure_auth is enabled by default on servers (it was disabled by default prior to this). This option causes the server to block connections by clients that attempt to use accounts that have passwords stored in the old (pre-4.1) format. It also causes clients to refuse to attempt a connection using the older password format. To bypass this restriction in the client, pass the option :secure_auth => false to Mysql2::Client.new(). If you using ActiveRecord, your database.yml might look something like this:
135+
136+
```
137+
development:
138+
adapter: mysql2
139+
encoding: utf8
140+
database: my_db_name
141+
username: root
142+
password: my_password
143+
host: 127.0.0.1
144+
port: 3306
145+
secure_auth: false
146+
````
147+
130148
## Cascading config
131149
132150
The default config hash is at:

ext/mysql2/client.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,11 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
716716
retval = &boolval;
717717
break;
718718

719+
case MYSQL_SECURE_AUTH:
720+
boolval = (value == Qfalse ? 0 : 1);
721+
retval = &boolval;
722+
break;
723+
719724
default:
720725
return Qfalse;
721726
}
@@ -1085,6 +1090,10 @@ static VALUE set_ssl_options(VALUE self, VALUE key, VALUE cert, VALUE ca, VALUE
10851090
return self;
10861091
}
10871092

1093+
static VALUE set_secure_auth(VALUE self, VALUE value) {
1094+
return _mysql_client_options(self, MYSQL_SECURE_AUTH, value);
1095+
}
1096+
10881097
static VALUE initialize_ext(VALUE self) {
10891098
GET_CLIENT(self);
10901099

@@ -1153,6 +1162,7 @@ void init_mysql2_client() {
11531162
rb_define_private_method(cMysql2Client, "write_timeout=", set_write_timeout, 1);
11541163
rb_define_private_method(cMysql2Client, "local_infile=", set_local_infile, 1);
11551164
rb_define_private_method(cMysql2Client, "charset_name=", set_charset_name, 1);
1165+
rb_define_private_method(cMysql2Client, "secure_auth=", set_secure_auth, 1);
11561166
rb_define_private_method(cMysql2Client, "ssl_set", set_ssl_options, 5);
11571167
rb_define_private_method(cMysql2Client, "initialize_ext", initialize_ext, 0);
11581168
rb_define_private_method(cMysql2Client, "connect", rb_connect, 7);

lib/mysql2/client.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def initialize(opts = {})
2222
initialize_ext
2323

2424
# Set MySQL connection options (each one is a call to mysql_options())
25-
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout].each do |key|
25+
[:reconnect, :connect_timeout, :local_infile, :read_timeout, :write_timeout, :secure_auth].each do |key|
2626
next unless opts.key?(key)
2727
case key
28-
when :reconnect, :local_infile
28+
when :reconnect, :local_infile, :secure_auth
2929
send(:"#{key}=", !!opts[key])
3030
when :connect_timeout, :read_timeout, :write_timeout
3131
send(:"#{key}=", opts[key].to_i)

0 commit comments

Comments
 (0)