Project

General

Profile

Database Server Installation Shared » History » Version 82

Anchi Cheng, 09/07/2022 05:14 PM

1 1 Amber Herold
h1. Database Server Installation
2
3
h2. Install MySQL 
4
5
The following is for the computer that hosts the databases. This involves installing MySQL server and creation/configuration of the leginondb and projectdb databases.
6
7 38 Amber Herold
8 1 Amber Herold
 
9 42 Amber Herold
h3. 1 Install MySQL-Server and MySQL-Client
10 38 Amber Herold
11
*Note:* You may already have MySQL Server and Client installed. Check by typing mysql at the command line. 
12
If you see a MySQL prompt (mysql>), you may skip this step.
13 1 Amber Herold
14 15 Neil Voss
To install Mysql on Linux you have two options (the first option is better):
15 1 Amber Herold
16 15 Neil Voss
# Use your package installer (yum, zypper, YaST, apt-get). For example:
17 52 Eric Hou
   <pre>sudo yum install mysql mysql-server</pre>
18
   For Suse
19
   <pre>yast2 -i mysql mysql-client</pre>
20 15 Neil Voss
# Download the latest MySQL-server package for Linux from http://www.mysql.com
21 1 Amber Herold
22 42 Amber Herold
h3. 2 Locate Example MySQL configuration files 
23 1 Amber Herold
24 42 Amber Herold
They are usually located in /usr/share/mysql.
25
26 1 Amber Herold
<pre>
27
ls /usr/share/mysql/my*
28 15 Neil Voss
    /usr/share/mysql/my-huge.cnf
29
    /usr/share/mysql/my-innodb-heavy-4G.cnf
30
    /usr/share/mysql/my-large.cnf
31
    /usr/share/mysql/my-medium.cnf
32
    /usr/share/mysql/my-small.cnf
33 20 Neil Voss
</pre>
34
If that does not work try the locate function
35
<pre>
36 15 Neil Voss
locate my | egrep "\.cnf$"
37
    /etc/my.cnf
38 1 Amber Herold
    /usr/share/mysql/my-huge.cnf
39
    /usr/share/mysql/my-innodb-heavy-4G.cnf
40
    /usr/share/mysql/my-large.cnf
41
    /usr/share/mysql/my-medium.cnf
42
    /usr/share/mysql/my-small.cnf
43 3 Amber Herold
</pre>
44 1 Amber Herold
45 42 Amber Herold
h3. 3 Configure my.cnf in /etc using my-huge.cnf as the template
46 1 Amber Herold
47 18 Neil Voss
# Copy my-huge.cnf to my.cnf
48 19 Neil Voss
<pre>sudo cp -v /usr/share/mysql/my-huge.cnf /etc/my.cnf</pre>
49 22 Neil Voss
# Edit /etc/my.cnf to add or change query cache variables like these (be sure to place them under the @[mysqld]@ section):
50 1 Amber Herold
<pre>
51 39 Amber Herold
query_cache_type = 1
52 1 Amber Herold
query_cache_size = 100M
53 21 Neil Voss
query_cache_limit= 100M
54 1 Amber Herold
</pre>
55 76 Anchi Cheng
# Search for the text default_storage_engine in /etc/my.cnf.  *VERY IMPORTANT* *If it exists and is set to other than MyISAM, you should change it* to:
56
<pre>default_storage_engine=MyISAM</pre>
57 10 Neil Voss
58 42 Amber Herold
h3. 4 Start the MySQL Server
59 1 Amber Herold
60 78 Patrick Goetz
Most modern linux systems use systemd:
61 10 Neil Voss
62 1 Amber Herold
<pre>
63 78 Patrick Goetz
sudo systemctl start mysql
64
</pre> or 
65
<pre>
66
sudo systemctl start mysqld
67
</pre>
68
69
70
For CentOS/Fedora/RHEL v. 6.x systems use the service command:
71
72
<pre>
73 1 Amber Herold
sudo /sbin/service mysqld start
74
</pre>
75
76 78 Patrick Goetz
For older Unix systems:
77 10 Neil Voss
78
<pre>
79
sudo /etc/init.d/mysqld start
80
</pre>
81
82 53 Eric Hou
or on some installations (Suse),
83 1 Amber Herold
84
<pre>
85 10 Neil Voss
sudo /etc/init.d/mysql start
86 1 Amber Herold
</pre>
87
88
For future reference: start | stop | restart MySQL Server with similar commands:
89
90 54 Eric Hou
For Centos, Fedora
91 1 Amber Herold
<pre>
92 10 Neil Voss
sudo /etc/init.d/mysqld start
93
sudo /etc/init.d/mysqld stop
94
sudo /etc/init.d/mysqld restart
95 54 Eric Hou
</pre>
96
or
97
<pre>
98 1 Amber Herold
sudo /sbin/service mysqld start
99 10 Neil Voss
sudo /sbin/service mysqld stop
100
sudo /sbin/service mysqld restart
101 1 Amber Herold
</pre>
102 54 Eric Hou
or for Suse
103
<pre>
104
sudo /etc/init.d/mysql start
105
sudo /etc/init.d/mysql stop
106
sudo /etc/init.d/mysql restart
107
</pre>
108 1 Amber Herold
109
h3. 5 Configure MySQL to start automatically at boot
110 78 Patrick Goetz
111
<pre>
112
sudo systemctl enable mysql
113
</pre> or
114
<pre>
115
sudo systemctl enable mysqld
116
</pre> depending on what the systemd service file is called for your system.
117
118 1 Amber Herold
119 79 Patrick Goetz
For CentOS v. 6.x systems:
120 1 Amber Herold
<pre>
121 23 Neil Voss
sudo /sbin/chkconfig mysqld on
122 79 Patrick Goetz
</pre> or for older SuSe systems:
123 1 Amber Herold
<pre>
124 54 Eric Hou
sudo /sbin/chkconfig --add mysql
125 23 Neil Voss
</pre>
126 1 Amber Herold
127 42 Amber Herold
h3. 6 For future reference, the database location will be:
128 1 Amber Herold
129 10 Neil Voss
<pre>
130
ls /var/lib/mysql
131 1 Amber Herold
    ibdata1  ib_logfile0  ib_logfile1  mysql  mysql.sock  test
132
</pre>
133
134 42 Amber Herold
h3. 7 Create the Leginon database, call it leginondb
135 3 Amber Herold
136 10 Neil Voss
<pre>
137 1 Amber Herold
sudo mysqladmin create leginondb
138
</pre>
139
140 42 Amber Herold
h3. 8 Create the Project database, call it projectdb
141 1 Amber Herold
142
<pre>
143
sudo mysqladmin create projectdb
144
</pre>
145
146 42 Amber Herold
h3. 9 Connect to mysql db
147 11 Neil Voss
148 40 Amber Herold
If starting from scratch, the mysql root user will have no password. This is assumed to be the case and we will set it later.
149 1 Amber Herold
150 23 Neil Voss
<pre>
151 28 Neil Voss
mysql -u root mysql
152 23 Neil Voss
</pre>
153 1 Amber Herold
154 40 Amber Herold
You should see a mysql prompt: mysql>
155
156
You can view the current mysql users with the following command.
157 11 Neil Voss
<pre>
158 40 Amber Herold
select user, password, host from user;
159 1 Amber Herold
      +------+----------+-----------+
160
      | user | password | host      |
161
      +------+----------+-----------+
162
      | root |          | localhost |
163
      | root |          | host1     |
164
      |      |          | host1     |
165
      |      |          | localhost |
166
      +------+----------+-----------+
167
      4 rows in set (0.00 sec)
168
</pre>
169
170 42 Amber Herold
h3. 10 Create user
171 1 Amber Herold
172 41 Amber Herold
Create and grant privileges to a user called usr_object for the databases on both the localhost and other hosts involved. For example, use wild card '%' for all hosts. You can set specific (@ALTER, CREATE, DROP, DELETE, INSERT, RENAME, SELECT, UPDATE@) privileges or @ALL@ privileges to the user. See MySQL Reference Manual for details. The following examples demonstrate some of the options available.
173 3 Amber Herold
174 67 Amber Herold
* *Option 1: More secure - restrict Drop and Delete privileges*
175
&nbsp;
176
At the mysql prompt execute the following commands:
177 1 Amber Herold
<pre>
178 36 Amber Herold
CREATE USER usr_object@'localhost' IDENTIFIED BY 'YOUR PASSWORD';
179 77 Patrick Goetz
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE ON leginondb.* TO 'usr_object'@'localhost';
180
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE ON projectdb.* TO 'usr_object'@'localhost';
181 63 Eric Hou
</pre>
182 62 Anchi Cheng
183 81 Sargis Dallakyan
Note: For MySQL version 8 use the following command instead to create a user:
184
<pre>
185
CREATE USER usr_object@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR PASSWORD';
186
</pre>
187
Thanks to - https://stackoverflow.com/questions/49963383/authentication-plugin-caching-sha2-password 
188 67 Amber Herold
* *Option 2: Less secure - allow all privileges*
189
&nbsp;
190 62 Anchi Cheng
You may choose to use the following less secure version (no password and all privileges) of the commands above, however, we recommend not allowing the DROP privilege on all tables nor DELETE privilege on most tables.
191 67 Amber Herold
&nbsp;
192 62 Anchi Cheng
At the mysql prompt execute the following commands:
193
<pre>
194 36 Amber Herold
CREATE USER usr_object@'localhost';
195 82 Anchi Cheng
GRANT ALL PRIVILEGES ON leginondb.* TO 'usr_object'@'localhost';
196
GRANT ALL PRIVILEGES ON projectdb.* TO 'usr_object'@'localhost';
197 36 Amber Herold
</pre>
198 1 Amber Herold
199 67 Amber Herold
* *Option 3: Allow access from all computers in the domain*
200
&nbsp;
201 41 Amber Herold
You may also choose to assign a domain to your commands and use a wildcard to allow access from all computers in the domain. 
202 67 Amber Herold
&nbsp;
203 1 Amber Herold
<pre>
204 62 Anchi Cheng
CREATE USER usr_object@'%.mydomain.edu' IDENTIFIED BY 'YOUR PASSWORD';
205 82 Anchi Cheng
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE ON leginondb.* to 'usr_object'@'%.mydomain.edu';
206
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE ON projectdb.* to 'usr_object'@'%.mydomain.edu';
207 62 Anchi Cheng
</pre>
208
209
 
210 3 Amber Herold
211 42 Amber Herold
h3. 11 Give create and access privileges for the processing databases which begin with "ap".
212 10 Neil Voss
213 24 Neil Voss
<pre>
214 27 Neil Voss
# if your web host is local
215 82 Anchi Cheng
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE, DELETE ON `ap%`.* to 'usr_object'@localhost; 
216 27 Neil Voss
# for all other hosts if you are accessing the databases from another computer
217 82 Anchi Cheng
GRANT ALTER, CREATE, INSERT, SELECT, UPDATE, DELETE ON `ap%`.* to 'usr_object'@'%.mydomain.edu';       
218 1 Amber Herold
</pre>
219
220 42 Amber Herold
h3. 12 Change Root password
221 1 Amber Herold
222 28 Neil Voss
To set the root password use the command: 
223 1 Amber Herold
224 28 Neil Voss
<pre>
225
sudo mysqladmin -u root password NEWPASSWORD
226
</pre>
227 1 Amber Herold
228 28 Neil Voss
Or you can do it from within mysql
229 10 Neil Voss
<pre>
230 36 Amber Herold
update user set password=password('your_own_root_password') where user="root";
231 1 Amber Herold
Query OK, 2 rows affected (0.01 sec)
232
Rows matched: 2  Changed: 2  Warnings: 0
233
234 43 Amber Herold
# run the flush privileges command to avoid problems
235 36 Amber Herold
flush privileges;
236
^D or exit;
237 3 Amber Herold
</pre>
238 1 Amber Herold
239 3 Amber Herold
240 1 Amber Herold
From now on, you will need to specify the password to connect to the database as root user like this:
241
242 30 Neil Voss
<pre>
243 36 Amber Herold
mysql -u root -p mysql
244 1 Amber Herold
</pre>
245
246 42 Amber Herold
h3. 13 Check MySQL variables
247 1 Amber Herold
248 31 Neil Voss
<pre>
249 44 Amber Herold
# at the command prompt, log into the leginon database
250
251 36 Amber Herold
mysql -u usr_object -p leginondb
252 1 Amber Herold
253 44 Amber Herold
# At the mysql prompt show variables that begin with 'query'.
254
# Check that the changes you made to my.cfg are in place.
255
256 36 Amber Herold
SHOW VARIABLES LIKE 'query%';
257 1 Amber Herold
      +------------------------------+-----------+
258
      | Variable_name                | Value     |
259
      +------------------------------+-----------+
260
      | ft_query_expansion_limit     | 20        |
261
      | have_query_cache             | YES       |
262
      | long_query_time              | 10        |
263
      | query_alloc_block_size       | 8192      |
264 44 Amber Herold
      | query_cache_limit            | 104857600 | ---This should correspond to your change
265 1 Amber Herold
      | query_cache_min_res_unit     | 4096      |
266 44 Amber Herold
      | query_cache_size             | 104857600 | ---This should correspond to your change
267
      | query_cache_type             | ON        | ---This should correspond to your change
268 1 Amber Herold
      | query_cache_wlock_invalidate | OFF       |
269
      | query_prealloc_size          | 8192      |
270
      +------------------------------+-----------+
271
      10 rows in set (0.00 sec)
272 36 Amber Herold
273 1 Amber Herold
exit;
274
</pre>
275 42 Amber Herold
276 61 Amber Herold
 If you do not see your changes, try restarting mysql. 
277
 On centOS: 
278 60 Amber Herold
<pre>
279
sudo /etc/init.d/mysqld restart
280
</pre>
281
282 1 Amber Herold
h3. 14 Make sure MySQL is running
283 3 Amber Herold
284 36 Amber Herold
<pre>
285 68 Amber Herold
mysqlshow -u root -p
286 1 Amber Herold
      +--------------+
287 30 Neil Voss
      | Databases    |
288
      +--------------+
289
      | mysql        |
290
      | leginondb    |
291 1 Amber Herold
      | projectdb    |
292
      +--------------+
293 30 Neil Voss
</pre>
294 36 Amber Herold
295 1 Amber Herold
h3. Run the following command from the command line:
296 30 Neil Voss
297 70 Amber Herold
Be sure to edit PASSWORD to the one you previously set for usr_object.
298 69 Amber Herold
299 46 Amber Herold
300 1 Amber Herold
<pre>
301
php -r "mysql_connect('localhost', 'usr_object', 'PASSWORD', 'leginondb'); echo mysql_stat();"; echo ""
302 30 Neil Voss
</pre>
303 1 Amber Herold
304
Expected output:
305
306
<pre>
307
Uptime: 1452562 Threads: 1 Questions: 618 Slow queries: 0 Opens: 117 Flush tables: 1 Open tables: 106 Queries per second avg: 0.000
308 47 Amber Herold
</pre>
309
310 48 Amber Herold
If there are any error messages, mysql may be configured incorrectly.
311
312 47 Amber Herold
*Note:* If you do not have php and php-mysql packages installed you need to install them to run the above command. The yum installation is:
313
<pre>
314
sudo yum -y install php php-mysql
315 36 Amber Herold
</pre>
316 74 Anchi Cheng
317
</pre>