0

I try to install sonarqube with mysql in docker.

  1. Mysql

```

$ docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root --name mysql mysql
$ docker exec -it mysql bash
# mysql -uroot -proot
mysql> CREATE DATABASE sonar CHARACTER SET utf8;
mysql> CREATE USER 'sonar'@'localhost' IDENTIFIED BY 'sonar' PASSWORD EXPIRE NEVER;
mysql> GRANT ALL PRIVILEGES ON sonar.* TO 'sonar'@'localhost';
mysql> FLUSH PRIVILEGES;

```

  1. SonarQube

```

$ docker run -d --name SonarQube --link mysql:mysql -p 9000:9000 -p 9092:9092 \
-e SONARQUBE_JDBC_USERNAME=sonar \
-e SONARQUBE_JDBC_PASSWORD=sonar \
-e "SONARQUBE_JDBC_URL=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true" \
  sonarqube

```

Here is my error log. Give me a solution. Thankyou

2018.08.10 12:53:37 ERROR web[][o.s.s.p.Platform] Web server startup failed java.lang.IllegalStateException: Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.'). at org.sonar.db.DefaultDatabase.checkConnection(DefaultDatabase.java:108) at org.sonar.db.DefaultDatabase.start(DefaultDatabase.java:75) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ...

SaeHyun Kim
  • 475
  • 2
  • 8
  • 26

1 Answers1

0

Change host in JDBC URL environment variable to below i.e replace localhost with mysql -

-e SONARQUBE_JDBC_URL=jdbc:mysql://mysql:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true"

Reason being you created a link to container mysql with DNS name mysql which means it will put a host entry for mysql container IP in sonarqube container. You can verify it by doing a docker exec & cat /etc/hosts in sonarqube container. That's how linking works w.r.t docker in the background.

PS - Localhost means the container itself & not the mysql container.

Update

Your second issue is, you are granting privileges for @'localhost' which means access from sonarqube container will be denied, you can change it to @'%' to allow from anywhere.

Ref - MySQL root access from all hosts
Allow all remote connections, MySQL

vivekyad4v
  • 13,321
  • 4
  • 55
  • 63
  • Thank you your answer. But I still get the same error. – SaeHyun Kim Aug 10 '18 at 13:19
  • Destroy all the containers & start fresh. It's supposed to work. If it doesn't post the `/etc/hosts` contents in your question after linking. – vivekyad4v Aug 10 '18 at 13:20
  • root@9b5ddc2ee125:/# cat /etc/hosts 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 172.17.0.3 9b5ddc2ee125 – SaeHyun Kim Aug 10 '18 at 13:24
  • It doesn't have a entry for mysql container. I have tried this & it has worked for me. Ref - `--link="" : Add link to another container (:alias or )` Doc - https://docs.docker.com/samples/library/mysql/#start-a-mysql-server-instance – vivekyad4v Aug 10 '18 at 13:30
  • I don't see any other issue here. I built it from scratch in my env & it's working for me. – vivekyad4v Aug 10 '18 at 13:34
  • Updated the answer. I tried to reproduce it & found a permission issue as well, have suggested a solution, give it a shot. – vivekyad4v Aug 10 '18 at 14:03
  • I have not solved it yet. But I understood your answer. THX – SaeHyun Kim Aug 11 '18 at 13:47