TechNote

とあるエンジニアのただのメモ

CentOS6.8にMySQLを入れる~ユーザ設定でハマる(Access denied for user 'root'@'localhost')

既にmysql関連が入っているか確認する

$ sudo yum list installed | grep mysql

実行結果

mysql-libs.x86_64 5.1.73-8.el6_8 @updates

何かあるので削除する。

$ sudo yum erase mysql-libs

または

$ sudo yum remove mysql*

実行結果

Loaded plugins: fastestmirror Setting up Remove Process Resolving Dependencies --> Running transaction check ---> Package mysql-libs.x86_64 0:5.1.73-8.el6_8 will be erased --> Processing Dependency: libmysqlclient.so.16()(64bit) for package: 2:postfix-2.6.6-6.el6_7.1.x86_64 --> Processing Dependency: libmysqlclient.so.16(libmysqlclient_16)(64bit) for package: 2:postfix-2.6.6-6.el6_7.1.x86_64 --> Processing Dependency: mysql-libs for package: 2:postfix-2.6.6-6.el6_7.1.x86_64 --> Running transaction check ---> Package postfix.x86_64 2:2.6.6-6.el6_7.1 will be erased --> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.x86_64 --> Running transaction check ---> Package cronie.x86_64 0:1.4.4-16.el6_8.2 will be erased --> Processing Dependency: cronie = 1.4.4-16.el6_8.2 for package: cronie-anacron-1.4.4-16.el6_8.2.x86_64 --> Running transaction check ---> Package cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 will be erased --> Processing Dependency: /etc/cron.d for package: crontabs-1.10-33.el6.noarch --> Restarting Dependency Resolution with new changes. --> Running transaction check ---> Package crontabs.noarch 0:1.10-33.el6 will be erased --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Removing: mysql-libs x86_64 5.1.73-8.el6_8 @updates 4.0 M Removing for dependencies: cronie x86_64 1.4.4-16.el6_8.2 @updates 174 k cronie-anacron x86_64 1.4.4-16.el6_8.2 @updates 43 k crontabs noarch 1.10-33.el6 @anaconda-CentOS-201605220104.x86_64/6.8 2.4 k postfix x86_64 2:2.6.6-6.el6_7.1 @anaconda-CentOS-201605220104.x86_64/6.8 9.7 M Transaction Summary ================================================================================ Remove 5 Package(s) Installed size: 14 M Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Erasing : cronie-anacron-1.4.4-16.el6_8.2.x86_64 1/5 Erasing : crontabs-1.10-33.el6.noarch 2/5 Erasing : cronie-1.4.4-16.el6_8.2.x86_64 3/5 Erasing : 2:postfix-2.6.6-6.el6_7.1.x86_64 4/5 Erasing : mysql-libs-5.1.73-8.el6_8.x86_64 5/5 Verifying : crontabs-1.10-33.el6.noarch 1/5 Verifying : 2:postfix-2.6.6-6.el6_7.1.x86_64 2/5 Verifying : cronie-anacron-1.4.4-16.el6_8.2.x86_64 3/5 Verifying : cronie-1.4.4-16.el6_8.2.x86_64 4/5 Verifying : mysql-libs-5.1.73-8.el6_8.x86_64 5/5 Removed: mysql-libs.x86_64 0:5.1.73-8.el6_8 Dependency Removed: cronie.x86_64 0:1.4.4-16.el6_8.2 cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 crontabs.noarch 0:1.10-33.el6 postfix.x86_64 2:2.6.6-6.el6_7.1 Complete!

公式yumリポジトリを追加

sudo yum install http://dev.mysql.com/get/mysql57-community-release-el6-8.noarch.rpm

実行結果

Loaded plugins: fastestmirror Setting up Install Process mysql57-community-release-el6-8.noarch.rpm | 8.9 kB 00:00 Examining /var/tmp/yum-root-B290r4/mysql57-community-release-el6-8.noarch.rpm: mysql57-community-release-el6-8.noarch Marking /var/tmp/yum-root-B290r4/mysql57-community-release-el6-8.noarch.rpm to be installed Loading mirror speeds from cached hostfile * base: ftp.yz.yamagata-u.ac.jp * epel: mirror.rise.ph * extras: ftp.yz.yamagata-u.ac.jp * remi-safe: mirror.smartmedia.net.id * updates: ftp.yz.yamagata-u.ac.jp Resolving Dependencies --> Running transaction check ---> Package mysql57-community-release.noarch 0:el6-8 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================== Package Arch Version Repository Size ================================================================================================== Installing: mysql57-community-release noarch el6-8 /mysql57-community-release-el6-8.noarch 8.2 k Transaction Summary ================================================================================================== Install 1 Package(s) Total size: 8.2 k Installed size: 8.2 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql57-community-release-el6-8.noarch 1/1 Verifying : mysql57-community-release-el6-8.noarch 1/1 Installed: mysql57-community-release.noarch 0:el6-8 Complete!

インストール

sudo yum -y install mysql-community-server

実行結果

Loaded plugins: fastestmirror Setting up Install Process Loading mirror speeds from cached hostfile * base: ftp.yz.yamagata-u.ac.jp * epel: mirror.rise.ph * extras: ftp.yz.yamagata-u.ac.jp * remi-safe: mirror.innosol.asia * updates: ftp.yz.yamagata-u.ac.jp mysql57-community | 2.5 kB 00:00 mysql57-community/primary_db | 102 kB 00:00 Resolving Dependencies --> Running transaction check ---> Package mysql-community-server.x86_64 0:5.7.17-1.el6 will be installed --> Processing Dependency: mysql-community-common(x86-64) = 5.7.17-1.el6 for package: mysql-community-server-5.7.17-1.el6.x86_64 --> Processing Dependency: mysql-community-client(x86-64) >= 5.7.9 for package: mysql-community-server-5.7.17-1.el6.x86_64 --> Running transaction check ---> Package mysql-community-client.x86_64 0:5.7.17-1.el6 will be installed --> Processing Dependency: mysql-community-libs(x86-64) >= 5.7.9 for package: mysql-community-client-5.7.17-1.el6.x86_64 ---> Package mysql-community-common.x86_64 0:5.7.17-1.el6 will be installed --> Running transaction check ---> Package mysql-community-libs.x86_64 0:5.7.17-1.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================== Package Arch Version Repository Size ================================================================================================== Installing: mysql-community-server x86_64 5.7.17-1.el6 mysql57-community 151 M Installing for dependencies: mysql-community-client x86_64 5.7.17-1.el6 mysql57-community 23 M mysql-community-common x86_64 5.7.17-1.el6 mysql57-community 328 k mysql-community-libs x86_64 5.7.17-1.el6 mysql57-community 2.1 M Transaction Summary ================================================================================================== Install 4 Package(s) Total download size: 177 M Installed size: 879 M Downloading Packages: (1/4): mysql-community-client-5.7.17-1.el6.x86_64.rpm | 23 MB 00:38 (2/4): mysql-community-common-5.7.17-1.el6.x86_64.rpm | 328 kB 00:00 (3/4): mysql-community-libs-5.7.17-1.el6.x86_64.rpm | 2.1 MB 00:03 (4/4): mysql-community-server-5.7.17-1.el6.x86_64.rpm | 151 MB 04:09 -------------------------------------------------------------------------------------------------- Total 618 kB/s | 177 MB 04:52 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : mysql-community-common-5.7.17-1.el6.x86_64 1/4 Installing : mysql-community-libs-5.7.17-1.el6.x86_64 2/4 Installing : mysql-community-client-5.7.17-1.el6.x86_64 3/4 Installing : mysql-community-server-5.7.17-1.el6.x86_64 4/4 Verifying : mysql-community-server-5.7.17-1.el6.x86_64 1/4 Verifying : mysql-community-client-5.7.17-1.el6.x86_64 2/4 Verifying : mysql-community-common-5.7.17-1.el6.x86_64 3/4 Verifying : mysql-community-libs-5.7.17-1.el6.x86_64 4/4 Installed: mysql-community-server.x86_64 0:5.7.17-1.el6 Dependency Installed: mysql-community-client.x86_64 0:5.7.17-1.el6 mysql-community-common.x86_64 0:5.7.17-1.el6 mysql-community-libs.x86_64 0:5.7.17-1.el6 Complete!

バージョン確認

mysqld --version

実行結果

mysqld Ver 5.7.17 for Linux on x86_64 (MySQL Community Server (GPL))

起動

sudo service mysqld start

何もせずrootでログインしようとすると...

mysql -u root

実行結果

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

一度停止する

sudo service mysqld stop

オプション付きで起動

sudo mysqld_safe --skip-grant-tables &

mysql> use mysql;
Database changed
mysql> select * from  user;

2件くらいユーザあるが、一度すべて消そう。

mysql> truncate table user;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on *.* to root@localhost identified by 'パスワード' with grant option;
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

一度quitし、下記で入りなおす。

mysql -u root -pパスワード 

いけた!

標準テキスト CentOS 7 構築・運用・管理パーフェクトガイド

標準テキスト CentOS 7 構築・運用・管理パーフェクトガイド

CentOS 7実践ガイド (impress top gear)

CentOS 7実践ガイド (impress top gear)