概要
yumでインストール
MySQL5.6
CentOS6系の場合
$ sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
$ sudo yum -y install mysql mysql-devel mysql-server mysql-utilities
$ sudo rpm -qa | grep mysql
$ sudo mysql --version
CentOS7系の場合
$ sudo yum -y install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
$ sudo yum -y install mysql mysql-devel mysql-server mysql-utilities
$ sudo rpm -qa | grep mysql
$ sudo mysql --version
MySQL5.7
CentOS6系の場合
$ sudo yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm
$ sudo yum -y install mysql mysql-devel mysql-server mysql-utilities
CentOS7系の場合
$ sudo yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
$ sudo yum -y install mysql mysql-devel mysql-server mysql-utilities
repoが追加されているか確認
$ sudo yum repolist
起動と自動起動設定
CentOS6系の場合
$ sudo chkconfig mysqld on
$ sudo /etc/init.d/mysqld start
CentOS7系の場合
$ sudo systemctl enable mysqld
$ sudo systemctl start mysqld
補足
新しいレポジトリを読みに行ってくれない場合は下記コマンドでキャッシュを削除してみてください。
$ sudo yum clean all
mysqlユーザー作成方法メモ
デフォルトのrootのパスワード
MySQL5.7.6以降の場合は、初回は下記に書かれているrootのパスワードを使う。
/var/log/mysqld.logにデフォルトのrootのパスワードが書いてあるので、初回はそれを使う。
$ sudo head /var/log/mysqld.log
*snip*
[Note] A temporary password is generated for root@localhost: xxxx
rootのパスワード設定
- mysql_secure_installationで設定
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
# 現在のmysqlのrootパスワードを入力、初回は空でエンター
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
# rootパスワードを設定しますか?
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
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? [Y/n] 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.
# リモートでのrootログインを拒否しますか?
Disallow root login remotely? [Y/n] 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? [Y/n] y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
# 今すぐFLUSH PRIVILEGESしますか?
Reload privilege tables now? [Y/n] y
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
- sql文で設定
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MyNewPass');
一般ユーザー
- 一般ユーザー作成
CREATE USER myuser@'localhost' IDENTIFIED BY 'MyNewPass';
- 権限設定
GRANT ALL ON DBName.* TO user@'localhost';
GRANT ALL ON DBName.* TO user@'%' IDENTIFIED BY 'password';
mysql5.7
こんな記事を見つけたのでとりあえずメモ
MySQL5.7のvalidate_passwordとかいうクソ機能殺す
文字コードの設定
デフォルトの文字コード設定を確認
文字コード設定がデフォルトだとlatin1が混じっているため、utf8に設定する。
mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
/etc/my.cnfに下記を追記する
/etc/my.cnf
[client]
default-character-set=utf8
[mysqld]
skip-character-set-client-handshake
character-set-server = utf8
collation-server = utf8_general_ci
init-connect = SET NAMES utf8
[mysqldump]
default-character-set=utf8
[mysql]
default-character-set=utf8
注意
下記の「誤」の記述方法は、MySQL5.6(5.5〜)から使用できなくなったので、「正」の記述で書く。
誤
[mysqld]
default-character-set=utf8
正
[mysqld]
character-set-server = utf8
mysqld再起動
CentOS6
$ sudo /etc/init.d/mysqld restart
CentOS7
$ sudo systemctl restart mysqld
文字コード設定が変わったことを確認
mysql> show variables like "char%";
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
mysql-clientだけインストールする
CentOS7.x
$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
$ sudo yum install -y mysql-community-client
mac
$ brew install mysql-client
ここにインストールされる
$ ls -d /usr/local/opt/mysql-client
/usr/local/opt/mysql-client
$ ls /usr/local/opt/mysql-client/bin/mysql
/usr/local/opt/mysql-client/bin/mysql