shotnの心の無垢ままに

主にプログラミングなどのIT関連の記事を心の向くままに書いていくブログです。

Windowsの無線LANアダプタを有効/無効を変更するバッチを作成する

今回は趣向を変えてWindows環境でのお話です。

バッチを作成しようと思った背景

仕事上ではもっぱらWindowsを使用しており、今までは有線のLANしか使えない環境にいました。(セキュリティとか申請とかの関係)

しかし、最近になって必要だろうということで無線LANを使えるようにしてもらいましたが、この無線が厄介で無線をしようとしたあとに席に戻って有線に切り替えたつもりが、「あれ?全然つながらないな・・・」と思ったらいつまでもつながっており少し困っていました。

LANの優先順位を変えたりもしてみたのですが、うまくいかないので「有効/無効」を切り替えることで対応することにしました。

しかし、コンパネ開くの遅い、無線切れてるか確認するのも面倒ということでバッチを作って対応することにしました。

要件は以下の感じ。


<要件> 1. 無線の有効/無効は1つのバッチで管理する

  1. 有効の場合は無効に、有効の場合は無効にする

1. 状態を確認できるようにし、その結果を確認した上で有効/無効の切り替えを選べるようにする。

というわけで以下のように作成してみました。

続きを読む

Macの環境変数を消してしまった場合

好奇心のあまり.bash_profileを簡略化して書けるのではと思いPATHを変更して試して見たところ... sourceも実行してしまっていたため、viすら開けなくなってしまった\(^o^)/オワタ

ということで復旧します。。。

.bash_profileの内容

初期の環境変数は以下のような感じです。

多少ユーザにより変わるところがあると思います。

HOME="/Users/UserName"
LANG="ja_JP.UTF-8"
LOGNAME="UserName"
OLDPWD
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:"
PWD="/Users/UserName"
SHELL="/bin/bash"
SHLVL="1"
SSH_AUTH_SOCK="/private/tmp/com.apple.launchd.nOkDl6eznB/Listeners"
TERM="xterm-256color"
TERM_PROGRAM="Apple_Terminal"
TERM_PROGRAM_VERSION="400"
TERM_SESSION_ID="EDDA8B1B-7D7F-409C-AD85-C2AAA35B7AE5"
TMPDIR="/var/folders/c9/yt_mxkkn4sz69h6d2rp7gds00000gn/T/"
USER="UserName"
XPC_FLAGS="0x0"
XPC_SERVICE_NAME="0"
続きを読む

次の日になったらMySQLが起動しなくなった!?

昨日に引き続きMySQLです。

事件発生!!

インストールした後PCを落として、今日いよいよ使おうと思ってMySQLに接続を試みたところ以下のエラーが発生ました。

$ mysql -uroot -p
-bash: mysql: command not found

またPATHが通ってないんですね…

よくよく調べてみるとターミナルでexportを使用して環境変数を設定すると、そのターミナル内だけで使用でき、再起動後などは使用できなくなるようです。

Windowsでいうところの「SET」コマンドと同じですね!

試しにexportコマンドで環境変数を設定した後に、ターミナルをもう一つ開いてecho $PATHで環境変数を見てください。exportコマンドを実行したターミナルと結果が違うはずです!

続きを読む

Mac環境にMySQLをインストールする

初めまして、shotnです。

最近自宅でプログラミングの勉強を始めたので、ログとして残しておこうかなと思い、ブログを始めることにしました。

今回はありふれていますが、MySQLをインストールしたいと思います。 ありふれている内容をなぜ書くかというと参考にさせていただいたページの通りにやってもうまくできず、一人でハマっていたからです・・・ 他の人からすれば「当然だろ!?」と思う方もいるかもしれませんが、わからないから調べてるのに手順省くなーo(`ω´ )oと言いたくなったので、自分で書きます!

それでは、いってみましょう!

前提

  • タイトルにもありますが、OSはMacを使用します。現在使用しているのは、「macOS High Sieerra(10.13.1)」です。
  • 既にHomebrewの設定は完了させてしまっているため、Homebrewの公式ページを参照していただければと思います。

brew.sh

1. HomebrewからMySQLをインストールする

  • 以下のコマンドを実行し、Homebrewの更新を行います。

$ brew update

  • 次に以下のコマンドを実行し、MySQLをインストールします。

$ brew install mysql

以下のような結果が出力されました。

==> Downloading https://homebrew.bintray.com/bottles/mysql-5.7.20.high_sierra.bo
Already downloaded: /Users/admin/Library/Caches/Homebrew/mysql-5.7.20.high_sierra.bottle.tar.gz
==> Pouring mysql-5.7.20.high_sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink include/mysql
/usr/local/include is not writable.

You can try again using:
  brew link mysql
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
    mysql_secure_installation

MySQL is configured to only allow connections from localhost by default

To connect run:
    mysql -uroot

To have launchd start mysql now and restart at login:
  brew services start mysql        ← ##次の手順##
Or, if you don't want/need a background service you can just run:
  mysql.server start            ← ##その次の手順##
==> Summary
🍺  /usr/local/Cellar/mysql/5.7.20: 324 files, 233.7MB

とりあえず、インストールは成功したようです。 既にダウンロード済みだったようで、少し初回時と出ているログが違うと思います。

このビールはなんなんだろうか・・・

よーし、インストール成功したし、一杯やっちゃうかー!!!

的な感じですか?インストール完了したくらいで一杯飲んでたらいつまでたっても進みゃーしねーw

2. サービスを起動する

ここまで来ちゃえばもうこっちのものですね!

次のコマンドを実行し、サービスを開始します。

$ brew services start mysql

以下の結果が出力されました。

Error: Permission denied @ rb_sysopen - /Users/admin/Library/LaunchAgents/homebrew.mxcl.mysql.plist

あら、エラーになってしまいましたね。原因はパーミッションなので権限なので、ではルート権限 (管理者権限)で実行してみましょう。 パスワードの入力を促されますので、パスワードを入力してください。

$ sudo brew services start mysql

Service `mysql` already started, use `brew services restart mysql` to restart.

何ですと・・・ここまでしてやったのに「もう既に起動してますけど・・・?」だと? まあいい、今日のとこr(ry

3. MySQLへの接続

一先ずこれができれば、クリアですかね? 以下のコマンドを実行し、MySQLへ接続します。

$ mysql -uroot

<結果>

-bash: mysql: command not found

あら?コマンドが・・見つからない・・・だと!? 今回実はここでハマりました・・・ よくよく考えればすぐわかるような気もするんですが、これPATHが通っていないだけなんですよね・・・

というわけでPATHを通します!

まずどこにMySQLがインストールされているか確認します。

$ brew list mysql

<結果>

/usr/local/Cellar/mysql/5.7.20/.bottle/etc/my.cnf
/usr/local/Cellar/mysql/5.7.20/bin/innochecksum
/usr/local/Cellar/mysql/5.7.20/bin/lz4_decompress
/usr/local/Cellar/mysql/5.7.20/bin/my_print_defaults
/usr/local/Cellar/mysql/5.7.20/bin/myisam_ftdump
/usr/local/Cellar/mysql/5.7.20/bin/myisamchk
/usr/local/Cellar/mysql/5.7.20/bin/myisamlog
/usr/local/Cellar/mysql/5.7.20/bin/myisampack
/usr/local/Cellar/mysql/5.7.20/bin/mysql
/usr/local/Cellar/mysql/5.7.20/bin/mysql.server
/usr/local/Cellar/mysql/5.7.20/bin/mysql_client_test
/usr/local/Cellar/mysql/5.7.20/bin/mysql_client_test_embedded
/usr/local/Cellar/mysql/5.7.20/bin/mysql_config
/usr/local/Cellar/mysql/5.7.20/bin/mysql_config_editor
/usr/local/Cellar/mysql/5.7.20/bin/mysql_embedded
/usr/local/Cellar/mysql/5.7.20/bin/mysql_install_db
/usr/local/Cellar/mysql/5.7.20/bin/mysql_plugin
/usr/local/Cellar/mysql/5.7.20/bin/mysql_secure_installation
/usr/local/Cellar/mysql/5.7.20/bin/mysql_ssl_rsa_setup
/usr/local/Cellar/mysql/5.7.20/bin/mysql_tzinfo_to_sql
/usr/local/Cellar/mysql/5.7.20/bin/mysql_upgrade
/usr/local/Cellar/mysql/5.7.20/bin/mysqladmin
/usr/local/Cellar/mysql/5.7.20/bin/mysqlbinlog
/usr/local/Cellar/mysql/5.7.20/bin/mysqlcheck
/usr/local/Cellar/mysql/5.7.20/bin/mysqld
/usr/local/Cellar/mysql/5.7.20/bin/mysqld_multi
/usr/local/Cellar/mysql/5.7.20/bin/mysqld_safe
/usr/local/Cellar/mysql/5.7.20/bin/mysqldump
/usr/local/Cellar/mysql/5.7.20/bin/mysqldumpslow
/usr/local/Cellar/mysql/5.7.20/bin/mysqlimport
/usr/local/Cellar/mysql/5.7.20/bin/mysqlpump
/usr/local/Cellar/mysql/5.7.20/bin/mysqlshow
/usr/local/Cellar/mysql/5.7.20/bin/mysqlslap
/usr/local/Cellar/mysql/5.7.20/bin/mysqltest
/usr/local/Cellar/mysql/5.7.20/bin/mysqltest_embedded
/usr/local/Cellar/mysql/5.7.20/bin/mysqlxtest
/usr/local/Cellar/mysql/5.7.20/bin/perror
/usr/local/Cellar/mysql/5.7.20/bin/replace
/usr/local/Cellar/mysql/5.7.20/bin/resolve_stack_dump
/usr/local/Cellar/mysql/5.7.20/bin/resolveip
/usr/local/Cellar/mysql/5.7.20/bin/zlib_decompress
/usr/local/Cellar/mysql/5.7.20/COPYING-test
/usr/local/Cellar/mysql/5.7.20/homebrew.mxcl.mysql.plist
/usr/local/Cellar/mysql/5.7.20/include/mysql/ (110 files)
/usr/local/Cellar/mysql/5.7.20/lib/libmysqlclient.20.dylib
/usr/local/Cellar/mysql/5.7.20/lib/pkgconfig/mysqlclient.pc
/usr/local/Cellar/mysql/5.7.20/lib/plugin/ (48 files)
/usr/local/Cellar/mysql/5.7.20/lib/ (4 other files)
/usr/local/Cellar/mysql/5.7.20/README-test
/usr/local/Cellar/mysql/5.7.20/scripts/mysql_install_db
/usr/local/Cellar/mysql/5.7.20/share/aclocal/mysql.m4
/usr/local/Cellar/mysql/5.7.20/share/doc/ (3 files)
/usr/local/Cellar/mysql/5.7.20/share/man/ (42 files)
/usr/local/Cellar/mysql/5.7.20/share/mysql/ (61 files)
/usr/local/Cellar/mysql/5.7.20/support-files/ (4 files)

今回は「/usr/local/Cellar/mysql/5.7.20/bin」にインストールされているようですので、これをPATHに設定します。

$ export PATH=$PATH:/usr/local/Cellar/mysql/5.7.20/bin/

さてこれでPATHが通ったはずなので、もう一度さっきのコマンドを実行してみます。

$ mysql -uroot

<結果>

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20 Homebrew

Copyright (c) 2000, 2017, 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を抜けます。

$ quit

次に以下のコマンドを入力します。

$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No:       ←ポリシーを適用するかどうかを聞いているので、使用するなら[y|Y]を入力しましょう。

There are three levels of password validation policy:

LOW    Length >= 8                              ←8文字以上
MEDIUM Length >= 8, numeric, mixed case, and special characters         ←8文字以上英数字以外1文字以上
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file ←8文字以上英数字以外1文字辞書に登録されている文字は使用できない(ような感じだと思う)

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:       ←ポリシーをどれにするか、「0」が無難でしょう。
Please set the password for root here.

New password:                       ←rootのパスワードを設定

Re-enter new password:                   ←再入力

Estimated strength of the password: 100           ←多分パスワード強度0が一番低い
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :  ←これでよければ[y|Y]を入力
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) :    ←誰でも入れるanonymousユーザを削除していいか聞かれているので、[y|Y]を入力して削除しましょう。

Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :   ←rootでリモートログインしていいか聞かれているのですが、セキュリティ的によくないので[y|Y]を入力しましょう。
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) :   ←テストDBは使わないので[y|Y]を入力しましょう。
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :     ←設定を即反映するか聞かれているので、[y|Y]を入力しましょう。
Success.

All done! 

設定したパスワードでログインしてみましょう

$ mysql -uroot -p
Enter password: 

ログインできると以下が出力されます。

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.20 Homebrew

Copyright (c) 2000, 2017, 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が使用できるようになりました。 そのほか細かい設定などもありますが、まずはログインできるようにしてみましょう。